How to read and process+output to file

This is a discussion on How to read and process+output to file within the C Programming forums, part of the General Programming Boards category; Hi, newbie here... I having some problem while read the data from a file and process it.. data file look ...

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    14

    How to read and process+output to file

    Hi, newbie here...

    I having some problem while read the data from a file and process it..

    data file look similar like this
    input(shop.txt)
    "
    Ninxo Department Store
    Customer Purchases October 2007

    Sim!celine 0123 18 F C 180
    clinton!mary 3476 41 F C 850
    lee!lester 2134 32 M E 400
    tai!june 2453 30 F C 320
    foster!liz 1199 27 F H 275


    "
    Need to read all files and process it to another output file with sum

    output file(shop.out)
    "
    Ninxo Department Store
    Customer Purchases October 2007

    Sim!celine 0123 18 F C 180
    clinton!mary 3476 41 F C 850

    Total amount spent = 1030


    "
    my code as below:
    encountered problem for reading in the data by seperate field..
    i did try to sscanf by define the '\n' ..but ..

    Code:
    while ((fgets(stemp, MAX, fp1))!=NULL)
    	{
    	  if('\n'>3)
    	  {
    	 sscanf(stemp, "%s %s %s %s %d", &cname,&age, &sex, &type,&amt);
    	  printf(">%s< >%s<  >%s<  >%s<  >%d<",cname,age,sex,type,amt);
    
    		}
    	  printf(">%s<", stemp);
    	  fprintf(fout,"%s",stemp);
    
    	 }
    [code]
    Last edited by jochen; 11-03-2007 at 11:59 AM.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,590
    Your input file seems to have 6 fields, yet you only scan for 5.

    Also, you need to read a few lines using another while loop to copy across the store name and date lines without trying to parse them at all.

    Your if() doesn't make any sense, but looking at the return result of sscanf() would.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    14
    thanks for pointing out the error in field..

    for checking '\n' within fgets..how can i check for that?

    i knew that my "if" doesn't make sense by simply compare '\n'>3...
    cause fgets gets string at a time...

    as i not really familiar with those function start with 'f' for file reading..

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,590
    Well given your use of sscanf(), there is no point in doing anything special with the newline at the end of the buffer. So detecting/removing it adds nothing to your code.

    Oh, I get it, the first 3 lines are the header (which you just print), and then the rest are parsed.

    Code:
    int lineNum =  0;
    while ((fgets(stemp, MAX, fp1))!=NULL) {
      ++lineNum;
      if ( lineNum > 3 ) {
        // sscanf here
      } else {
        // copy to outfile here
      }
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Sep 2007
    Posts
    14
    thanks again..

    i follow as what u mentioned ...work..
    but after i go into more details to read it into array for processing..program keep out/terminate

    Code:
    while ((fgets(stemp, MAX, fp1))!=NULL)
    	{
    	  /* if(p=strchr(stemp,'\n'))!= NULL)   */
    
    	  //if(stemp=='\n')
    	  
    	  //printf(">&#37;s<", stemp);
    	  fprintf(fout,"%s",stemp);
    	   
        line++;
        //printf("%d", line);
    	 if(line>=4)
        {
                         
    	   sscanf(stemp, "%s %s %s %s %s %s", &cname[arr], &id[arr] ,&age[arr], &sex[arr], &type[arr],&amt[arr]);
    	  printf("\n >%s< >%s< >%s<  >%s<  >%s<  >%s<",cname[arr],id[arr],age[arr],sex[arr],type[arr],amt[arr]);
          arr++;
          }
                  
    	  
    
    	 }
     set=0;
      
        printf("\n >%s< >%s< >%s<  >%s<  >%s<  >%s<",cname[set],id[set],age[set],sex[set],type[set],amt[set]);   /* try to check and print out the specific array contents */
    just curious...for sscanf...those data can not read into &field?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 02-06-2009, 11:27 AM
  2. program compiles, runs, but produces no output
    By happyclown in forum C Programming
    Replies: 11
    Last Post: 01-28-2009, 04:43 AM
  3. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM
  4. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 02:58 PM
  5. Simple File Creation Algorithm
    By muffin in forum C Programming
    Replies: 13
    Last Post: 08-24-2001, 03:28 PM

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