Is it possible to read numbers and strings of same line with..

S

srikanth

i have a text file like below,

test.txt file (actually my test file file is with 10000 lines but here
i tested with 3 lines)

3 06.09.2006 16:37:25
3 06.09.2006 16:40:02
3 06.09.2006 16:42:31

i want to read this and output as it looks but iam getting abnormal
results like this and i used fscanf in my code,

RESULT:

num: 3 date: .09.2006
time:16:37:2516:40:0216:42:31
num: 3 date: 06.09.200606.09.2006 time:16:40:0216:42:31
num: 3 date: 06.09.2006 time:16:42:31

my code is
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
char testDatestr[3][10];
char testTimeStr[3][8];

fp = fopen( "test.txt", "r");

while(fgetc(fp) != EOF)
{
fscanf(fp, "%d %s
%s",&SeriNumber,&testDatestr[0],&testTimeStr[0]);
i++;
}

fclose(fp);

for(j = 0; j<3; j++)
printf("\n num: %d date: %s
time:%s",SeriNumber,testDatestr[j],testTimeStr[j] );
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


could anyone can please suggest me how can i read numbers and strings
of a file and output as it is.

Thanks alot,
Regards,
Srikanth
 
T

tmp123

srikanth said:
i have a text file like below,

test.txt file (actually my test file file is with 10000 lines but here
i tested with 3 lines)

3 06.09.2006 16:37:25
3 06.09.2006 16:40:02
3 06.09.2006 16:42:31

i want to read this and output as it looks but iam getting abnormal
results like this and i used fscanf in my code,

RESULT:

num: 3 date: .09.2006
time:16:37:2516:40:0216:42:31
num: 3 date: 06.09.200606.09.2006 time:16:40:0216:42:31
num: 3 date: 06.09.2006 time:16:42:31

my code is
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
char testDatestr[3][10];
char testTimeStr[3][8];

fp = fopen( "test.txt", "r");

while(fgetc(fp) != EOF)
{
fscanf(fp, "%d %s
%s",&SeriNumber,&testDatestr[0],&testTimeStr[0]);
i++;
}

fclose(fp);

for(j = 0; j<3; j++)
printf("\n num: %d date: %s
time:%s",SeriNumber,testDatestr[j],testTimeStr[j] );
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


could anyone can please suggest me how can i read numbers and strings
of a file and output as it is.

Thanks alot,
Regards,
Srikanth



C strings finish in a '\0'. That means that store "hello" needs a[6],
not a[5].

Kind regards.

PS: better testDatestr than &testDatestr[0]-
 
M

mark_bluemel

srikanth said:
i have a text file like below,

test.txt file (actually my test file file is with 10000 lines but here
i tested with 3 lines)

3 06.09.2006 16:37:25
3 06.09.2006 16:40:02
3 06.09.2006 16:42:31

i want to read this and output as it looks but iam getting abnormal
results like this and i used fscanf in my code,

I thought I'd spotted your problem... :) But I hadn't totally.

IMHO, fscanf() is one of the warts on the face of C...
RESULT:

num: 3 date: .09.2006
time:16:37:2516:40:0216:42:31
num: 3 date: 06.09.200606.09.2006 time:16:40:0216:42:31
num: 3 date: 06.09.2006 time:16:42:31

my code is
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
char testDatestr[3][10];

I have now... How long is a date?
char testTimeStr[3][8];

And how long is a time? (Hint: How are strings represented in C? How do
we know they've finished?)
fp = fopen( "test.txt", "r");

while(fgetc(fp) != EOF)
OK - you've read a character here, what do you do with it?
{
fscanf(fp, "%d %s
%s",&SeriNumber,&testDatestr[0],&testTimeStr[0]);
i++;
}

fclose(fp);

for(j = 0; j<3; j++)
printf("\n num: %d date: %s
time:%s",SeriNumber,testDatestr[j],testTimeStr[j] );
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


could anyone can please suggest me how can i read numbers and strings
of a file and output as it is.


My initial feeling was, I think, at least part right.

I wouldn't use fscanf, it's not easy enough to use correctly. You'd do
better to read a line at a time into a buffer, then use sscanf() to
parse it. Don't forget to use the right size for your strings..

See the faq on scanf() etc for some discussion...
http://c-faq.com/stdio/index.html
 
M

mark_bluemel

tmp123 said:
srikanth said:
i have a text file like below,

test.txt file (actually my test file file is with 10000 lines but here
i tested with 3 lines)

3 06.09.2006 16:37:25
3 06.09.2006 16:40:02
3 06.09.2006 16:42:31

i want to read this and output as it looks but iam getting abnormal
results like this and i used fscanf in my code,

RESULT:

num: 3 date: .09.2006
time:16:37:2516:40:0216:42:31
num: 3 date: 06.09.200606.09.2006 time:16:40:0216:42:31
num: 3 date: 06.09.2006 time:16:42:31

my code is
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
char testDatestr[3][10];
char testTimeStr[3][8];

fp = fopen( "test.txt", "r");

while(fgetc(fp) != EOF)
{
fscanf(fp, "%d %s
%s",&SeriNumber,&testDatestr[0],&testTimeStr[0]);
i++;
}

fclose(fp);

for(j = 0; j<3; j++)
printf("\n num: %d date: %s
time:%s",SeriNumber,testDatestr[j],testTimeStr[j] );
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


could anyone can please suggest me how can i read numbers and strings
of a file and output as it is.

Thanks alot,
Regards,
Srikanth



C strings finish in a '\0'. That means that store "hello" needs a[6],
not a[5].


Insufficient... The program would still be broken.
 
M

mark_bluemel

srikanth said:
my code is
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
char testDatestr[3][10];
char testTimeStr[3][8];

fp = fopen( "test.txt", "r");

while(fgetc(fp) != EOF)
{
fscanf(fp, "%d %s
%s",&SeriNumber,&testDatestr[0],&testTimeStr[0]);
i++;
}

fclose(fp);

for(j = 0; j<3; j++)
printf("\n num: %d date: %s
time:%s",SeriNumber,testDatestr[j],testTimeStr[j] );
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


BTW - that doesn't compile. Please cut and paste a complete working (or
at least compiling) testcase next time. Some people won't bother adding
in the bits you've missed out.
 
C

CBFalconer

srikanth said:
my code is
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
char testDatestr[3][10];
char testTimeStr[3][8];

fp = fopen( "test.txt", "r");

while(fgetc(fp) != EOF)
{
fscanf(fp, "%d %s
%s",&SeriNumber,&testDatestr[0],&testTimeStr[0]);
i++;
}

fclose(fp);

for(j = 0; j<3; j++)
printf("\n num: %d date: %s
time:%s",SeriNumber,testDatestr[j],testTimeStr[j] );
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


BTW - that doesn't compile. Please cut and paste a complete working
(or at least compiling) testcase next time. Some people won't bother
adding in the bits you've missed out.


And don't use // comments in usenet, and do limit line length to
something under 72 chars, 67 is a useful objective.
 
K

Keith Thompson

I wouldn't use fscanf, it's not easy enough to use correctly. You'd do
better to read a line at a time into a buffer, then use sscanf() to
parse it. Don't forget to use the right size for your strings..

Yes, reading a line at a time and parsing it with sscanf() is likely
to be better than using fscanf().

*But* both fscanf() and sscanf() invoke undefined behavior on numeric
overflow, so scanning numeric values is dangerous *unless* you can
somehow guarantee that nothing is going to overflow. (Overflow could
be caused by something as simple as a transmission glitch that drops
the blank between two consecutive large integers; sscanf() gives you
no way to recover from such an error, or even to detect it.) This is
IMHO a serious flaw.

One thing you can do is use sscanf() to parse the input line into
strings (using a maximum field width with each "%s"), and then use
something like strtol() to parse the string.

Or you can roll your own parsing code.
 
C

CBFalconer

Keith said:
.... snip ...

*But* both fscanf() and sscanf() invoke undefined behavior on
numeric overflow, so scanning numeric values is dangerous *unless*
you can somehow guarantee that nothing is going to overflow.
(Overflow could be caused by something as simple as a transmission
glitch that drops the blank between two consecutive large integers;
sscanf() gives you no way to recover from such an error, or even to
detect it.) This is IMHO a serious flaw.

One thing you can do is use sscanf() to parse the input line into
strings (using a maximum field width with each "%s"), and then use
something like strtol() to parse the string.

Or you can roll your own parsing code.

See my post in "if string is a number".
 
P

pete

srikanth said:
i have a text file like below,

test.txt file (actually my test file file is with 10000 lines but here
i tested with 3 lines)

3 06.09.2006 16:37:25
3 06.09.2006 16:40:02
3 06.09.2006 16:42:31

i want to read this and output as it looks but iam getting abnormal
results like this and i used fscanf in my code,

RESULT:

num: 3 date: .09.2006
time:16:37:2516:40:0216:42:31
num: 3 date: 06.09.200606.09.2006 time:16:40:0216:42:31
num: 3 date: 06.09.2006 time:16:42:31

my code is
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
char testDatestr[3][10];
char testTimeStr[3][8];

fp = fopen( "test.txt", "r");

while(fgetc(fp) != EOF)
{
fscanf(fp, "%d %s
%s",&SeriNumber,&testDatestr[0],&testTimeStr[0]);
i++;
}

fclose(fp);

for(j = 0; j<3; j++)
printf("\n num: %d date: %s
time:%s",SeriNumber,testDatestr[j],testTimeStr[j] );
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

could anyone can please suggest me how can i read numbers and strings
of a file and output as it is.


/* BEGIN new.c */
/*
** There are only three different values
** that can be assigned to rc
** from the fscanf calls in this program.
** They are:
** EOF
** 0
** 1
** If rc equals EOF, then the end of file was reached,
** or there is some input problem;
** and ferror and feof can be used to distinguish which.
** If rc equals 0, then an empty line was entered
** and the array contains garbage values.
** If rc equals 1, then there is a string in the array.
** Up to LENGTH number of characters are read
** from a line of a text stream
** and written to a string in an array.
** If the line is longer than LENGTH,
** then the extra characters are discarded.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

#define FILE_NAME "test.txt"
#define LENGTH 32
#define str(x) # x
#define xstr(x) str(x)
#define SPACE " \t\r\n\f\v"

int main(void)
{
int rc;
char array[LENGTH + 1];
FILE *fp;
char *fn, *p, *p1, *p2;
const unsigned min = sizeof "3 06.09.2006 16:42:31";
/* 01234567890123456789012345678901 */

if (min > sizeof array) {
puts("min > sizeof array");
printf("%u > %d\n", min, LENGTH + 1);
exit(EXIT_SUCCESS);
}
fn = FILE_NAME;
fp = fopen(fn, "r");
if (fp == NULL) {
printf("fopen problem with %s.\n", fn);
exit(EXIT_SUCCESS);
}
printf("%s is open.\n\n", fn);
for (;;) {
rc = fscanf(fp, "%" xstr(LENGTH) "[^\n]%*[^\n]", array);
if (!feof(fp)) {
getc(fp);
}
if (rc == 0) {
array[0] = '\0';
}
if (rc == EOF) {
break;
}
p = array;
if (strlen(p) != min - 1) {
puts("Unexpectedly short line found.\n"
"This isn't the right file format.");
break;
}
if (p[10] != '.' || p[13] != '.' ||
p[26] != ':' || p[29] != ':')
{
puts("Dots and colons not found "
"where they're supposed to be.\n"
"This isn't the right file format.");
break;
}
if (isspace(p[7]) == 0 || isspace(p[23]) == 0) {
puts("Space characters not found "
"where they're supposed to be.\n"
"This isn't the right file format.");
break;
}
if (isdigit(*p) == 0) {
puts("Frist character not a digit.\n"
"This isn't the right file format.");
break;
}
do {
++p;
} while (isdigit(*p) != 0);
*p++ = '\0';
while (isspace(*p) != 0) {
++p;
}
p1 = p++;
while (*p != '\0' && isspace(*p) == 0) {
++p;
}
*p++ = '\0';
while (isspace(*p) != 0) {
++p;
}
p2 = p;
printf("num: %s date: %s time: %s\n", array, p1, p2);
}
fclose(fp);
printf("\n%s is closed.\n", fn);
return 0;
}

/* END new.c */
 
P

pete

srikanth said:
i have a text file like below,

test.txt file (actually my test file file is with 10000 lines but here
i tested with 3 lines)

3 06.09.2006 16:37:25
3 06.09.2006 16:40:02
3 06.09.2006 16:42:31

i want to read this and output as it looks but iam getting abnormal
results like this and i used fscanf in my code,

RESULT:

num: 3 date: .09.2006
time:16:37:2516:40:0216:42:31
num: 3 date: 06.09.200606.09.2006 time:16:40:0216:42:31
num: 3 date: 06.09.2006 time:16:42:31

/* BEGIN new.c */

#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <string.h>

#define FILE_NAME "test.txt"
#define SPACE " \t\r\n\f\v"
#define NMEMB(A) (sizeof (A) / sizeof *(A))

int
get_delimeters(char **lineptr, size_t *n, char *s, FILE *stream);

int main(void)
{
size_t n;
int rc;
char *field[] = {"num","date","time"};
size_t buff_size = 0;
char *buff_ptr = NULL;
char *fn = FILE_NAME;
FILE *fp = fopen(fn, "r");

if (fp == NULL) {
printf("fopen problem with %s.\n", fn);
exit(EXIT_SUCCESS);
}
printf("%s is open for reading.\n\n", fn);
do {
for (n = 0; n != NMEMB(field); ++n) {
do {
rc = get_delimeters
(&buff_ptr, &buff_size, "\n" SPACE, fp);
} while (rc == 1);
if (1 > rc) {
break;
}
printf("%s: %s ", field[n], buff_ptr);
}
putchar('\n');
} while (rc > 0);
if (rc == 0) {
puts("realloc problem in get_delimeters.");
}
free(buff_ptr);
puts("buff_ptr has been freed.");
fclose(fp);
printf("%s is closed.\n", fn);
return 0;
}

int get_delimeters(char **lineptr, size_t *n, char *s, FILE *stream)
{
int rc;
void *p;
size_t count;

count = 0;
while ((rc = getc(stream)) != EOF) {
++count;
if (count + 2 > *n) {
p = realloc(*lineptr, count + 2);
if (p == NULL) {
if (*n > count) {
(*lineptr)[count] = '\0';
(*lineptr)[count - 1] = (char)rc;
} else {
if (*n == 1) {
**lineptr = '\0';
}
ungetc(rc, stream);
}
count = 0;
break;
}
*lineptr = p;
*n = count + 2;
}
if (strchr(s, rc) != NULL) {
(*lineptr)[count - 1] = '\0';
break;
}
(*lineptr)[count - 1] = (char)rc;
}
if (rc != EOF) {
rc = count > INT_MAX ? INT_MAX : count;
} else {
if (*n > count) {
(*lineptr)[count] = '\0';
}
}
return rc;
}
/* END new.c */
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,263
Messages
2,571,064
Members
48,769
Latest member
Clifft

Latest Threads

Top