954,498 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Read 1 line to 1 linked list

Here is the input.txt file that I have.

2 5 -2 3 1 2 4 0
1 2 1 0


These are 2 polynominals. 2x^5 - 2x^3 + x^2 + 4 and x^2 + 1 while <3,5> : 3 is the coefficient, 5 is the degree.

I just can separate 2 lines into 2 files input.txt & input1.txt and read from them.
I want to read the first line to 1 linked list and then the second line to another linked list. Can anyone help me with this?

void readfile(char *filename, List &s, Data &x)
{
	FILE *pfile;
	pfile=fopen(filename,"rt");
	if(pfile==NULL)
	{
		cout<<"Error reading!\n";
		exit(0);
	}
	while(!feof(pfile))
	{
		fscanf(pfile,"%d %d ",&x.coefficient,&x.degree);
		insertHead(s,x);
	}
	fclose(pfile);
}
void main()
{
        List s,s1;
	init(s);
	init(s1);
	Data x;
	readfile("input.txt",s,x);
	readfile("input1.txt",s1,x);

}
muffle
Newbie Poster
2 posts since Dec 2010
Reputation Points: 10
Solved Threads: 0
 

Make

void main()


this:

int main()
taylorc8
Newbie Poster
12 posts since Nov 2009
Reputation Points: 10
Solved Threads: 0
 

Yes, first you need to use C++ fstream, instead of C functions, and incorporate getline() command like so :

ifstream fileInput("input.txt");
if(!fileInput){ cerr << "File not found\n"; }
string line;
while(getline(fileInput,line)){
 cout << line << endl; //prints line by line
}
firstPerson
Senior Poster
3,923 posts since Dec 2008
Reputation Points: 841
Solved Threads: 608
 

I also want to put the line into the linked list by insertHead().

Ex: Read 2 5 -2 3 1 2 4 0 from input.txt, insertHead into the linked list 's'
Then read 1 2 1 0 from input.txt again and insert value into another linked list 's1'

How can I apply your method T^T All the number is int, not char.

muffle
Newbie Poster
2 posts since Dec 2010
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: