reading a string into a structure

This is a discussion on reading a string into a structure within the C Programming forums, part of the General Programming Boards category; what iv'e done so far is read data from a dat file into a string, then validated it by checking ...

  1. #1
    Registered User breed's Avatar
    Join Date
    Oct 2001
    Posts
    91

    reading a string into a structure

    what iv'e done so far is read data from a dat file into a string, then validated it by checking there was enough data to fill the feilds in the struct then passed the string to a function.
    here's where i fall over(doh!)
    I need to read a string into a structure, so that i can run further validation test on 2 of the 4 members

    switch(opt)
    {
    case 'I' :
    case 'R' : f_num = sscanf(buff, "%1c %5s %6s %4d", &I_R_RECORD.rec_type,&I_R_RECORD.cust_code,
    &I_R_RECORD.part_num, &I_R_RECORD.quantity);
    if(f_num != 4)
    error_rep(buff, error(5));
    else
    rpt(buff);

    /*here iv'e check for the correct amount of records*/
    /*and then passed the str to rpt();*/

    void rpt(char *buff)
    {

    char code[6], part[7];
    char *chk_digit, *chk_d;
    FILE *fp;


    while(fscanf(fp, "%[^\n]\n", buff) !=NULL)
    {
    while(fscanf(fp,"%1c%6s%7s%4d", &I_R_RECORD.rec_type,
    &I_R_RECORD.cust_code,
    &I_R_RECORD.part_num,
    &I_R_RECORD.quantity) != '\n')
    {
    /*i know this doesn't work so any pionters on how to solve it*/
    /*will be received with warm hands*/

    chk_d_cust_num(code);
    if(chk_digit)
    strncat(I_R_RECORD.cust_code, chk_digit, 6);
    else
    error_rep(buff, error(1));
    chk_d_part(part);
    if(chk_d)
    strncat(I_R_RECORD.part_num, chk_d, 7);
    else
    error_rep(buff, error(2));

    }
    }
    fputs(buff, valid_rec_fp);
    return;
    }
    Thanx in advs
    Before you judge a man, walk a mile in his
    shoes. After that, who cares.. He's a mile away and you've got
    his shoes.
    ************William Connoly

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,338
    You lost me

    Why do you sscanf on buff, then attempt to overwrite it with
    while(fscanf(fp, "%[^\n]\n", buff) !=NULL)

    Oh, and fscanf returns neither NULL nor '\n' (at least not as you've written them).

    Personally, I'd drop fscanf altogether, and just keep to reading the file using fgets, and decoding the line using sscanf

    Code:
    char buff[BUFSIZ];
    while ( fgets( buff, sizof(buff), fp ) != NULL ) {
        // do stuff with buff
        // sscanf etc
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C String Problem: Not reading end of string
    By sedavis4 in forum C Programming
    Replies: 5
    Last Post: 11-17-2008, 09:29 PM
  2. Custom String class gives problem with another prog.
    By I BLcK I in forum C++ Programming
    Replies: 1
    Last Post: 12-18-2006, 02:40 AM
  3. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 09:19 AM
  4. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  5. Reading a string from an input file into a structure
    By Maxwell25 in forum C++ Programming
    Replies: 15
    Last Post: 05-01-2003, 10:30 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21