C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 09-09-2007, 06:35 PM   #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
Mr. Deeds is offline   Reply With Quote
Old 09-09-2007, 08:20 PM   #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
Mr. Deeds is offline   Reply With Quote
Old 09-09-2007, 08:27 PM   #3
cas
Registered User
 
Join Date: Sep 2007
Posts: 452
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
cas is offline   Reply With Quote
Old 09-09-2007, 08:38 PM   #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???
Mr. Deeds is offline   Reply With Quote
Old 09-09-2007, 11:30 PM   #5
CSharpener
 
vart's Avatar
 
Join Date: Oct 2006
Posts: 5,336
using sscanf for example
__________________
If I have eight hours for cutting wood, I spend six sharpening my axe.
vart is offline   Reply With Quote
Old 09-10-2007, 11:24 PM   #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
Mr. Deeds is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Data Structure Eror prominababy C Programming 3 01-06-2009 09:35 AM
Storing # from Data File in Arrays? dakarn C Programming 7 11-13-2008 09:15 PM
Need Help: Storing numeric data into matrix from a data file spiit231 C Programming 3 02-26-2008 02:12 PM
Storing data from a file into a single char string tuxinator C Programming 16 05-01-2006 10:21 PM
simulate Grep command in Unix using C laxmi C Programming 6 05-10-2002 04:10 PM


All times are GMT -6. The time now is 05:21 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

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