0

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);

}

Edited by muffle: n/a

3
Contributors
3
Replies
4
Views
6 Years
Discussion Span
Last Post by muffle
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
}
0

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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.