Thread: Skipping records to text file and storing data.

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

    Skipping records to text file and storing data.

    Hello,

    I am completely new to C programming and
    I have a simple question that is giving me problems. This is my first time working with text files so I was hoping to get some support to quicken the learning process because I can't find much documentation on it. It seems everything is C++ on google.

    Anyways I have a .fil file that was created in notepad. It is lined up like a table with records. I have opened the file ok but I can't use fscanf properly to store the data into variables.

    I'm needing to skip the first line so I can get to the data. It seems no newline is processed??
    I tried adding \n to the document in hopes that the stream would recognize them and jump the pointer down to the next record. It seems to be processing all the reading all the white space instead? Here's a sample of the file:


    Code:
    First Name	Last Name   Sale Day	Length	  Width	       Height	State    Cost	Area 
    
    Sam 		Smith          N    12.5000     13.5000      8.00    GA    15.75	300  
    Sam 		Collins        N    15.0000     11.2500      8.00    AL    12.00	300
    How can I skip records or get the pointer where I need it?
    How can I get the data and store it into variables? Everything I have tried with the fscanf function has failed me. Can anyone help??? Much thanks to much appreciated help...


    Mr. Deeds

  2. #2
    Registered User
    Join Date
    Sep 2007
    Posts
    4
    Well I broke it down even simpler with this and I still am getting strange results full of zeros.

    Code:
    Sam Smith           N         12.5000   13.5000   8.00      GA        15.75     300       \n
    Sam Collins         N         15.0000   11.2500   8.00      AL        12.00     300       \n
    Cathy Smith         Y         10.5000   14.0000   9.00      SC        15.99     300       \n
    EOF
    and here's the code I've been using and trying to fix.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int main(void){
    
    	char ch;
    	float l;
    	float w;
    	float h;
    	int area;
    	char sDay;
    	char state[3];
    	char cName[21];
    	char line[94];
    	char buffer[94];
    
    	FILE *fp;
    	fp = fopen("txTDsale.fil", "r");
    	if(fp == NULL)
    		printf("could not open file\n");
    	setvbuf(fp, buffer, _IOLBF, 94);
    	//while((ch = getc(fp)) != '\n');
            //this one works fine for printing to screen but can't store data
            //fgets(line, sizeof(line), fp);
    	
    	//fscanf(fp, "%20[^\n]%10c%10f%10f%10f%10s%%10f%10d)", cName, &sDay, &l, &w, &h, &state, &area);
    	fscanf(fp, " %20[^\n] %c %f %f %f %s %f %d [^\n]", cName, &sDay, &l, &w, &h, &state, &area);
    	printf("%s %c %f %f %f %s %d %f %d\n", cName, sDay, l, w, h, state, area);
    	printf("%s\n", line);
    	//fgets(line, sizeof(line), fp);
            //printf("%s \n", line);
    	return 0;
    	}
    I have been having a terrible time getting scanf and fscanf functions to work.
    I don't know if I'm misinterpreting the syntax or what I'm doing wrong.
    Can anyone help?

    Mr. Deeds

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    I'm not entirely certain what you've tried because you didn't provide code [at the time I started writing this].

    If you want to skip a line of text, you can do so with something along the lines of:
    Code:
    int c;
    while( (c = getc(fp)) != EOF && c != '\n' ) ;
    Where fp is your FILE*. This reads from the file until EOF or newline is encountered. If c is EOF after the loop, no newline was found. What you want to do in such a case is up to you.

    Anyway, once that's done, I'd really recommend reading in lines with fgets() and parsing them after that. You can use sscanf(), paying attention to its return value. Alternatively you can split each line up with strtok(), converting numbers with strtol() and strtod() (or similar). This has the advantage of precise error checking; if a number can't be converted, the strto functions tell you why. And you can allocate as much memory as is needed for strings; or, alternatively, safely detect that the string is too long.

    The scanf() family of functions works best with rigidly formatted input (hence the `f' in the name). Invalid input can really throw them (and you) for a loop. If you have complete control over your input file, sscanf() can be pretty safely used.
    Last edited by cas; 09-09-2007 at 08:29 PM. Reason: Code was posted while I was replying

  4. #4
    Registered User
    Join Date
    Sep 2007
    Posts
    4
    Well I've gotten fgets() to work but how can I store my data in variables from that???

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    using sscanf for example
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  6. #6
    Registered User
    Join Date
    Sep 2007
    Posts
    4
    EHHH....

    Thanks a lot for all that helped me. For some reason I got lost thinking my strings were being read in binary and not accepting the \n\r. I was in a frenzy trying to get some last minute code to work for a project. I normally don't post such shrimpy problems but I'll be on here continuing to learn. Once again thanks for the replies...


    Mr. Deeds

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  2. Storing # from Data File in Arrays?
    By dakarn in forum C Programming
    Replies: 7
    Last Post: 11-13-2008, 09:15 PM
  3. Replies: 3
    Last Post: 02-26-2008, 02:12 PM
  4. Storing data from a file into a single char string
    By tuxinator in forum C Programming
    Replies: 16
    Last Post: 05-01-2006, 10:21 PM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM