943,783 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 1115
  • C++ RSS
You are currently viewing page 2 of this multi-page discussion thread; Jump to the first page
Nov 25th, 2008
0

Re: fstream problem.

Still can't figure it out.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
imput1234 is offline Offline
19 posts
since Oct 2008
Nov 25th, 2008
0

Re: fstream problem.

What about..this...
c++ Syntax (Toggle Plain Text)
  1. //...
  2. fin.open(ifile);
  3. if(fin.fail())
  4. std::cout<<"does not exist";
  5. //...
Reputation Points: 47
Solved Threads: 69
Posting Whiz
cikara21 is offline Offline
340 posts
since Jul 2008
Nov 25th, 2008
0

Re: fstream problem.

every text should cover this
C++ Syntax (Toggle Plain Text)
  1. fin.open ( "somefile.txt" );
  2. if( !fin ) // or if( fin.fail( ) )
  3. {
  4. //error handler goes here
  5. }
  6. else
  7. {
  8. //file opened successfully, start reading
  9. }
Reputation Points: 1268
Solved Threads: 228
Posting Virtuoso
vmanes is offline Offline
1,895 posts
since Aug 2007
Nov 25th, 2008
0

Re: fstream problem.

Click to Expand / Collapse  Quote originally posted by imput1234 ...
Nevermind, weird it works now. I havent;' made any changes.

But I do have another problem.

I'm trying to make the program so that, if the user does not enter a valid file name, it will output that and clsoe the program.

This is what I have so far, and no matter what the user enters, the console will go in to a failstate. Anyone know why

Thanks.

	ifstream fin;

	cout << "Enter filename: ";
	getline(cin, ifile);	

    
	if ( !ifile.size() == 0 )
	{	
		cout << "No file found." << endl;
		system ("PAUSE");
		return 1;
	}

	fin.open(ifile.c_str());

just try this one...........................

	ifstream fin;

	cout << "Enter filename: ";
	getline(cin, ifile);	
        fin.open(ifile.c_str());
       
	 if(fin.fail())
	{	
		cout << "No file found." << endl;
		system ("PAUSE");
		return 1;
	}

	fin.open(ifile.c_str());
Reputation Points: 9
Solved Threads: 1
Newbie Poster
rajenpandit is offline Offline
13 posts
since Aug 2008
Dec 9th, 2008
0

Re: fstream problem.

Hey guys,

I'm working on another program, and have run in to another problem.

Basically for this program, I read and extract information from a file. I do not know when the file is going to end, so I used a while loop to get my data.

This is all I have done, haven't proceeded since the loop doesn't work.
Hey guys,

I'm working on another program, and have run in to another problem.

Basically for this program, I read and extract information from a file. I do not know when the file is going to end, so I used a while loop to get my data.

This is all I have done, haven't proceeded since the loop doesn't work.


void extract ( string file_name, ifstream& fin )
{
	int i = 0;
	string temp;

	
	fin.open( file_name.c_str( ) );
	fin >> temp;
	cout << temp;
	
	while ( !fin.eof( ) )//THIS PART DOES NOT WORK I've also tried while (fin) while (!fin.fail( )) can't seem to get it to work
	{
		//STUFF
	
	}

What I want it to do is end the program, when there is no more info in the file.

Thanks.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
imput1234 is offline Offline
19 posts
since Oct 2008
Dec 10th, 2008
0

Re: fstream problem.

alright I figured that out.

It seems as though that method will only work when followed by

fin >> somevariable;

is there anyway I can use getline (fin, string);

instead?

Thanks.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
imput1234 is offline Offline
19 posts
since Oct 2008
Dec 10th, 2008
0

Re: fstream problem.

while ( getline ( fin, str ) )
{
//do any other processing
}

If the read action is successful, you enter the loop body. If the read action fails, loop ends.
Reputation Points: 1268
Solved Threads: 228
Posting Virtuoso
vmanes is offline Offline
1,895 posts
since Aug 2007
Dec 10th, 2008
0

Re: fstream problem.

I'm seem to have a LOT of problems with this program.
The loop issue is fixed, now it will not read all the numbers that exist.

The Main:

	string file1, temp[1000];
	float n1[1000], n2[1000];
	int i = 1, t;

	ifstream fin;
	ofstream out;
	
	cout << "\t\t Welcome to Grade Calculator!\n\n";
	cout << "What file has your scores on it: ";
	getline( cin, file1 );
	
	fin.open( file1.c_str( ) );
	out.open( "results.html" );

	if ( fin.fail( ) ||  file1.size( ) == 0 )
	{
		cout << "No file was found or entered." << endl;
		system ("PAUSE");
		return 1;
	}

	out << "<html>" << endl;
	out << "<body>" << endl;
	out << "<pre>"  << endl;
	out << "\t\tGrade Report" << endl;
	out << "Activity     Your Grade       Possible" << endl;

	while ( getline ( fin, temp[i] ) )
	{
		fin >> n1[i] >> n2[i];
		i++;
	}

	while ( i != 0 )
	{
		if (temp[i] == "end")
		{
			out << "Percentage" << setw(13) << getpart(fin, temp[i-1].substr(0, ' ')) << endl;
		}
		else if ( n1[i] != -1 || n2[i] != -1 )
		{
			out << setw(11) << temp[1] << setw(14) << n1[i] << setw(16) << n2[i] << endl;
		}

		i--;
	}

	out << "</html>" << endl;
	out << "</body>" << endl;
	out << "</pre>" << endl;

	fin.close( );
	out.close( );

	system ("PAUSE");

	return 0;

The getpart function

float getpart (ifstream& fin, string label)
{
	string temp;
	float score = 0, total = 0, average, tem, n1, n2; 
	
	while (getline ( fin, temp ) ) 
	{
		if (temp != "end")
			if (temp.substr( ) == label)
			{
				fin >> n1 >> n2;
				score = score + n1;
				total = total + n2;
			}
	}

	average = score / total;
	
	return average;
}

The file it is reading from
Quiz 1
3 5
Quiz 2
4 10
Attd. 11-23
10 10
Quiz 3
5 5
end
-1 -1
Lab 1
10 100
Lab 2
20 20
Lab 3
30 130
end
-1 -1
Test 1
145 150
Test 2
200 210
end
-1 -1
Program 1
40 50
Program 2
90 100
Program 3
100 100
Program 4
90 100
end
-1 -1
Lab Test 1
35 50
Lab Test 2
25 50
end
-1 -1
Final Exam
234 400
end
-1 -1

Basically what the program does is organize the grades in another file .html
ie
Quiz 1 score total
and then calculate the percent in the end of each section
Percent: 100
Last edited by imput1234; Dec 10th, 2008 at 9:11 pm.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
imput1234 is offline Offline
19 posts
since Oct 2008
Dec 10th, 2008
0

Re: fstream problem.

I ran some tests, and it looks like the array is only storing 2 variables, of which the first one is correct the second is wrong.

this is the part where I actually store information, anyone know whats wrong?

Thanks.

	while ( getline ( fin, temp[i] ) )
	{
		fin >> n1[i] >> n2[i];
		i++;
	}
Reputation Points: 10
Solved Threads: 0
Newbie Poster
imput1234 is offline Offline
19 posts
since Oct 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Need help with Tic Tac Toe
Next Thread in C++ Forum Timeline: SDL Performance





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC