fstream problem.

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Oct 2008
Posts: 19
Reputation: imput1234 is an unknown quantity at this point 
Solved Threads: 0
imput1234 imput1234 is offline Offline
Newbie Poster

Re: fstream problem.

 
0
  #11
Nov 25th, 2008
Still can't figure it out.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 338
Reputation: cikara21 is an unknown quantity at this point 
Solved Threads: 66
cikara21's Avatar
cikara21 cikara21 is offline Offline
Posting Whiz

Re: fstream problem.

 
0
  #12
Nov 25th, 2008
What about..this...
  1. //...
  2. fin.open(ifile);
  3. if(fin.fail())
  4. std::cout<<"does not exist";
  5. //...
.:-cikara21-:.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 1,679
Reputation: vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold 
Solved Threads: 193
vmanes's Avatar
vmanes vmanes is offline Offline
Posting Virtuoso

Re: fstream problem.

 
0
  #13
Nov 25th, 2008
every text should cover this
  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. }
Everyone's gotta believe in something. I believe I'll have another drink.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 13
Reputation: rajenpandit is an unknown quantity at this point 
Solved Threads: 1
rajenpandit rajenpandit is offline Offline
Newbie Poster

Re: fstream problem.

 
0
  #14
Nov 25th, 2008
Originally Posted by imput1234 View Post
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());
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 19
Reputation: imput1234 is an unknown quantity at this point 
Solved Threads: 0
imput1234 imput1234 is offline Offline
Newbie Poster

Re: fstream problem.

 
0
  #15
Dec 9th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 19
Reputation: imput1234 is an unknown quantity at this point 
Solved Threads: 0
imput1234 imput1234 is offline Offline
Newbie Poster

Re: fstream problem.

 
0
  #16
Dec 10th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 1,679
Reputation: vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold 
Solved Threads: 193
vmanes's Avatar
vmanes vmanes is offline Offline
Posting Virtuoso

Re: fstream problem.

 
0
  #17
Dec 10th, 2008
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.
Everyone's gotta believe in something. I believe I'll have another drink.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 19
Reputation: imput1234 is an unknown quantity at this point 
Solved Threads: 0
imput1234 imput1234 is offline Offline
Newbie Poster

Re: fstream problem.

 
0
  #18
Dec 10th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 19
Reputation: imput1234 is an unknown quantity at this point 
Solved Threads: 0
imput1234 imput1234 is offline Offline
Newbie Poster

Re: fstream problem.

 
0
  #19
Dec 10th, 2008
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++;
	}
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 899 | Replies: 18
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC