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

Discussion in 'C Programming' started by srikanth, Dec 22, 2006.

  1. srikanth

    srikanth Guest

    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
    srikanth, Dec 22, 2006
    #1
    1. Advertising

  2. srikanth

    tmp123 Guest

    srikanth wrote:
    > 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]-
    tmp123, Dec 22, 2006
    #2
    1. Advertising

  3. srikanth

    Guest

    srikanth wrote:
    > 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
    , Dec 22, 2006
    #3
  4. srikanth

    Guest

    tmp123 wrote:
    > srikanth wrote:
    > > 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.
    , Dec 22, 2006
    #4
  5. srikanth

    Guest

    srikanth wrote:

    > 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.
    , Dec 22, 2006
    #5
  6. srikanth

    CBFalconer Guest

    wrote:
    > srikanth wrote:
    >
    >> 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.

    --
    Chuck F (cbfalconer at maineline dot net)
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net>
    CBFalconer, Dec 22, 2006
    #6
  7. writes:
    [...]
    > 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.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
    Keith Thompson, Dec 22, 2006
    #7
  8. srikanth

    CBFalconer Guest

    Keith Thompson wrote:
    >

    .... 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".

    --
    Chuck F (cbfalconer at maineline dot net)
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net>
    CBFalconer, Dec 23, 2006
    #8
  9. srikanth

    pete Guest

    srikanth wrote:
    >
    > 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 */


    --
    pete
    pete, Dec 23, 2006
    #9
  10. srikanth

    pete Guest

    srikanth wrote:
    >
    > 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 */


    --
    pete
    pete, Jan 4, 2007
    #10
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Hugo
    Replies:
    10
    Views:
    1,292
    Matt Humphrey
    Oct 18, 2004
  2. kaushikshome
    Replies:
    4
    Views:
    753
    kaushikshome
    Sep 10, 2006
  3. scad
    Replies:
    23
    Views:
    1,146
    Alf P. Steinbach
    May 17, 2009
  4. Marek Stepanek
    Replies:
    12
    Views:
    401
    Peter J. Holzer
    Sep 2, 2006
  5. one man army

    Numbers to strings to numbers again

    one man army, Dec 28, 2005, in forum: Javascript
    Replies:
    6
    Views:
    133
    one man army
    Dec 30, 2005
Loading...

Share This Page