GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 24-Oct-2004, 01:08
krisbot krisbot is offline
New Member
 
Join Date: Oct 2004
Posts: 2
krisbot is on a distinguished road

Using getline to read in data


Hi, I've read these forums for help a lot of times, but this is my first time posting . Anyways, I'm looking for help on a problem I'm having reading in and storing data. The program is designed to read data in from a file, store the information for later calculations, and then save the data to an outfile. The data is information from the 2000 Presidential election, here is an example:

State Elec Vote Gore Bush Nader Buchanan
Alabama 9 695602 944409 18349 6364
Alaska 3 79004 167398 28747 5192

Opening the file and saving the data to a file is simple enough for me, but I'm perplexed on how to read the data in, and what variables to store it under. I've tried using the getline function in a while loop, but I can never figure the syntax out, and I just end up getting frustrated. Also, I'm confused on how to avoid storing the first line into the variables (State Elec Vote Gore Bush Nader Buchanan), so they won't get full of a bunch of char value garbage. If anyone could show me an example of how this data could be read in a stored, I'd be most appreciative. Thanks again.
  #2  
Old 24-Oct-2004, 07:45
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
Hello krisbot. Long time listener, first time caller huh? Welcome to GIDForums.

I know that your original question was about getline. I don't know if you were intent on using getline, but I had to answer this anyway. Throughout this forum, scanf has a pretty bad reputation (with reason), but this program is perfect for scanf (IMHO).

Anyway, I whipped this up real quick to show how this can be used:
CPP / C++ / C Code:
#include <stdio.h>

struct{
	char	state[30];
	int		electoral;
	long	gore;
	long	bush;
	long	nader;
	long	buchanan;
}vote[50];


int main(int argc, char* argv[])
{
	
	FILE* 	fp;
	int		loop;
	char	junk[80];
	
	if(argc!=2)
		printf("You must specify a file to read.\n");
	else{
		if( (fp=fopen(argv[1],"r") ) == NULL)
			printf("Could not open file: %s\n",argv[1]);
		else{
			fgets(junk,80,fp);			//Ignore first line.
			loop = 0;
			while(!feof(fp)){
				fscanf(fp,"%s %d %ld %ld %ld %ld",
							vote[loop].state,
							&(vote[loop].electoral),
							&(vote[loop].gore),
							&(vote[loop].bush),
							&(vote[loop].nader),
							&(vote[loop].buchanan) );
				loop++;
			}
			vote[loop].state[0] = (char)0;
			fclose(fp);
			printf("\nVoting\n");
			printf("State\tElectorals\tGore\tBush\tNader\tBuchanan\n");
			printf("-----\t----------\t----\t----\t-----\t--------\n");
			loop = 0;
			while( (loop<50) && (vote[loop].state[0]) ){
				printf("%s\t%d\t\t%ld\t%ld\t%ld\t%ld\n",
							vote[loop].state,
							vote[loop].electoral,
							vote[loop].gore,
							vote[loop].bush,
							vote[loop].nader,
							vote[loop].buchanan );
				loop++;
			}
			
		}
	}
	
	return 0;
} 

It's not very fancy and it is entirely static in storage. Notice that I just read and throw away the first line since I know what the catagories are...

My apologies if scanf is not what you want to use, but it is very good at reading structured data like this.

Good luck!
  #3  
Old 24-Oct-2004, 11:30
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,258
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
Quote:
Originally Posted by dsmith
I know that your original question was about getline. I don't know if you were intent on using getline, but I had to answer this anyway. Throughout this forum, scanf has a pretty bad reputation (with reason), but this program is perfect for scanf (IMHO).
Actually, IMAO scanf() has a bad reputation on pretty much ALL forums. That's because it's very difficult to control the input. And scanf() leaves junk in the input stream that messes up future reads. But fscanf() is a slightly different matter since you know (or should know) the format of the data at all reads.

But to be completely safe, change all fscanf()'s to
CPP / C++ / C Code:
      fgets(junk,80,fp);      //read a full line.
      sscanf(junk,"%s %d %ld %ld %ld %ld",
              vote[loop].state,
              &(vote[loop].electoral),
              &(vote[loop].gore),
              &(vote[loop].bush),
              &(vote[loop].nader),
              &(vote[loop].buchanan) );
This way you won't have to deal with any input issues dealing with extra characters left in the file like scanf() does with the input stream.
__________________

Got a cough? Go home tonight and eat a whole box of Ex-Lax. Tomorrow, you'll be afraid to cough.
-- Pearl Williams
  #4  
Old 24-Oct-2004, 13:49
krisbot krisbot is offline
New Member
 
Join Date: Oct 2004
Posts: 2
krisbot is on a distinguished road
Quote:
Originally Posted by dsmith
Hello krisbot. Long time listener, first time caller huh? Welcome to GIDForums.

I know that your original question was about getline. I don't know if you were intent on using getline, but I had to answer this anyway. Throughout this forum, scanf has a pretty bad reputation (with reason), but this program is perfect for scanf (IMHO).

Anyway, I whipped this up real quick to show how this can be used:
CPP / C++ / C Code:
#include <stdio.h>

struct{
	char	state[30];
	int		electoral;
	long	gore;
	long	bush;
	long	nader;
	long	buchanan;
}vote[50];


int main(int argc, char* argv[])
{
	
	FILE* 	fp;
	int		loop;
	char	junk[80];
	
	if(argc!=2)
		printf("You must specify a file to read.\n");
	else{
		if( (fp=fopen(argv[1],"r") ) == NULL)
			printf("Could not open file: %s\n",argv[1]);
		else{
			fgets(junk,80,fp);			//Ignore first line.
			loop = 0;
			while(!feof(fp)){
				fscanf(fp,"%s %d %ld %ld %ld %ld",
							vote[loop].state,
							&(vote[loop].electoral),
							&(vote[loop].gore),
							&(vote[loop].bush),
							&(vote[loop].nader),
							&(vote[loop].buchanan) );
				loop++;
			}
			vote[loop].state[0] = (char)0;
			fclose(fp);
			printf("\nVoting\n");
			printf("State\tElectorals\tGore\tBush\tNader\tBuchanan\n");
			printf("-----\t----------\t----\t----\t-----\t--------\n");
			loop = 0;
			while( (loop<50) && (vote[loop].state[0]) ){
				printf("%s\t%d\t\t%ld\t%ld\t%ld\t%ld\n",
							vote[loop].state,
							vote[loop].electoral,
							vote[loop].gore,
							vote[loop].bush,
							vote[loop].nader,
							vote[loop].buchanan );
				loop++;
			}
			
		}
	}
	
	return 0;
} 

It's not very fancy and it is entirely static in storage. Notice that I just read and throw away the first line since I know what the catagories are...

My apologies if scanf is not what you want to use, but it is very good at reading structured data like this.

Good luck!

What compiler is used for that code? I use Microsoft Visual Studio C++ Pro (got it for free). Here is an example of what my code generally looks like:
CPP / C++ / C Code:
#include<iostream>
#include<fstream>
#include<string>

using namespace std;

int main()
{	
	ifstream inFile;

	inFile.open("election.txt");

	if(!inFile) //Checking for inFile
	{
		cout << "File not found." << endl;
	}

	inFile.close();

	return 0;
}
That was just opening an infile and checking to see if it is there (for me anyways). I don't know if its just a different compiler or if I haven't got to that lvl of coding yet.
Last edited by dsmith : 24-Oct-2004 at 16:48.
  #5  
Old 24-Oct-2004, 16:57
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
Hi Krisbot. That is an ancient language known as "C"

It should compile on any about any C compiler, there is nothing exotic there at all.

As a personal preference, I like the stdio input/output much better than iostream & fstream. It is probably what I am used to, but to me, it is much easier to use.

That is why I put the comment about your possible attachment to getline before I posted the code. Anyway, I think this is a great illustration of the ease of use of the fscanf function. Perhaps someone that is more fond of the fstream library will post an alternate solution using that.
 
 

Recent GIDBlogStupid Management Policies by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
How to detect end of file with read() function call? nkhambal C Programming Language 6 12-Oct-2004 02:08
Transferring data between arrays butters C Programming Language 3 07-Jul-2004 23:55
[CONTEST?]Data Structure Test dsmith C Programming Language 2 06-Jun-2004 16:13
Automate a data change php form mjfmn MySQL / PHP Forum 4 20-Oct-2003 10:37

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 06:13.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.