| | |
fstream problem.
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
What about..this...
c++ Syntax (Toggle Plain Text)
//... fin.open(ifile); if(fin.fail()) std::cout<<"does not exist"; //...
every text should cover this
C++ Syntax (Toggle Plain Text)
fin.open ( "somefile.txt" ); if( !fin ) // or if( fin.fail( ) ) { //error handler goes here } else { //file opened successfully, start reading }
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.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
•
•
Join Date: Aug 2008
Posts: 13
Reputation:
Solved Threads: 1
•
•
•
•
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());•
•
Join Date: Oct 2008
Posts: 19
Reputation:
Solved Threads: 0
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.
What I want it to do is end the program, when there is no more info in the file.
Thanks.
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.
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.
{
//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.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
•
•
Join Date: Oct 2008
Posts: 19
Reputation:
Solved Threads: 0
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:
The getpart function
The file it is reading from
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
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.
•
•
Join Date: Oct 2008
Posts: 19
Reputation:
Solved Threads: 0
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.
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++;
}![]() |
Similar Threads
- fstream Tutorial (C++)
- read to end of line problem (C)
- fstream.h + winsock2.h doesn't work? (C++)
- need help with programing with fstream to find area of triangle (C++)
- File processing problem (C++)
- Stack Queue Fstream (C++)
- Mode 13 graphics,Problem with io streams and new operator (C++)
- ** Need Help ** in a small C++ problem (C++)
Other Threads in the C++ Forum
- Previous Thread: Need help with Tic Tac Toe
- Next Thread: SDL Performance
Views: 899 | Replies: 18
| Thread Tools | Search this Thread |
Tag cloud for C++
6 api application array arrays assignment beginner binary bitmap c++ c/c++ calculator char class classes code coding compile compiler console conversion convert count data database delete developer display dll email encryption error file forms fstream function functions game generator getline givemetehcodez graph homeworkhelper iamthwee ifstream image input int java lazy lib loop looping loops map math matrix memory multidimensional multiple newbie news node number output parameter pointer problem program programming project proxy python random read recursion recursive reference return sort sorting string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






