![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
|
|
#1 |
|
Unverified User
Join Date: Sep 2005
Posts: 209
Rep Power: 0
![]() |
More fileopen close stuff extending from my last thread!
I'm back quickly!
Well, if you read the last massive thread i started, then you'll be on track for where this is going. Basically, I'm doing a program that will read the file i made in the last thread, and then give you some options to change data in it. Before I can get into the fun part of parsing lines and what not, I at least need to get the data from the file. The following is what i have thus far: #include <iostream>
#include <fstream>
#include <string>
#include <cctype>
#include <iomanip>
#include <vector>
#include <cstdlib>
using namespace std;
int main()
{
int i=0,j=0,id[10];
vector<int> years;
char sex[10];
float wage[10];
int yearsworked;
ifstream infile;
ofstream outfile;
string disp1, disp2, filename;
float holder[10], line;
cout << "Please input the name of the file you wish to open: " ;
getline(cin, filename);
infile.open(filename.c_str());
if(infile.fail())
{
cout <<"\nThe file was not successfully opened." << endl;
exit(1);
}
getline(infile, disp1);
getline(infile, disp2);
while(getline( infile, line))
{
holder[i] = line;
i++;
}
infile.close();
cout << "\nThe file contained the following:" << endl;
cout << disp1 << endl << disp2 << endl;
for(j=0; j<10; j++)
{
cout << holder[i] << endl;
}
system("PAUSE");
return 0;
}It gives me the error: no matching function for call to `getline(std::ifstream&, float&)' which corresponds to: while(getline( infile, line)) I was hoping to read in the first to lines which are just labels for the table, and then store the next 10 lines in an array. From there i would extract data from each line and into a corresponding array like the last thread, and alter it that way, but i'll do that part AFTER i can successfully compile this code and read in the file..lol... I'm betting this one is simple. |
|
|
|
|
|
#2 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
I believe the getline function will only read into a std::string class, though I could be wrong on that. What we definitely know is that it won't read into a float. However, that's what the iostream operators are for:
while (infile >> line) |
|
|
|
|
|
#3 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Just a small comment on your terminal remark in the last thread: see this check, in your new code?
if(infile.fail())
{
cout <<"\nThe file was not successfully opened." << endl;
exit(1);
}int OMGBBQ!!!!!1111 (string catastrophe)
{
cerr << catastrophe << endl;
return 1;
} cin >> someVariableRequiringCorrectInput;
if (!cin.good ()) return OMGBBQ!!!!!1111 ("Bad user! Bad user!");
...
// more codeNow, as to your current question: did you read the types of arguments required by the various versions of 'getline', and what its return is? You can't coerce your libraries to have things they don't have just out of pure desire. If the writers didn't provide them, and you want them that badly, you have to write the yourself.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#4 |
|
Unverified User
Join Date: Sep 2005
Posts: 209
Rep Power: 0
![]() |
what exactly is cerr. I've seen it, but we haven't been over it.
|
|
|
|
|
|
#5 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Standard error, akin to stderr. One gets 3 file handles/pointers by default when the program opens. Standard error typically goes to the console, same as stdout, but they may be separately redirected (as to a log file and an output file), particularly in the case of batch files.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#6 |
|
Hobbyist
Join Date: Sep 2005
Posts: 266
Rep Power: 4
![]() |
I've changed your code to use the 'stringstream' object. While it's still not robust it should give you an idea of how to extract numeric input. The major changes are in bold:
#include <iostream>
#include <fstream>
#include <string>
#include <cctype>
#include <iomanip>
#include <vector>
#include <cstdlib>
#include <sstream>
using namespace std;
int main()
{
int id[10];
vector<int> years;
char sex[10];
float wage[10];
int yearsworked;
ifstream infile;
ofstream outfile;
string disp1, disp2, filename, line;
cout << "Please input the name of the file you wish to open: " ;
getline(cin, filename);
infile.open(filename.c_str());
if(infile.fail())
{
cout <<"\nThe file was not successfully opened." << endl;
exit(1);
}
getline(infile, disp1);
getline(infile, disp2);
vector<float> holder;
float temp;
while( getline(infile, line) )
{
stringstream ss ( stringstream::in | stringstream::out );
ss << line;
ss >> temp;
if ( !ss ) { continue; }
holder.push_back( temp );
}
infile.close();
cout << "\nThe file contained the following:" << endl;
cout << disp1 << endl << disp2 << endl;
for( int i = 0; i < holder.size(); ++i )
{
cout << holder[i] << endl;
}
cout << "Press ENTER to exit program. . .";
cin.sync();
cin.get();
return 0;
} |
|
|
|
|
|
#7 |
|
Unverified User
Join Date: Sep 2005
Posts: 209
Rep Power: 0
![]() |
Well,
I made Ooble's change above, and it now reads the files, and gives me the first 2 lables of the table correctly, but when i try to store the rest to an array, it gives me junk. How should i declare that array and the "line" variable that each line is first stored to so that it gives me exactly what the file contains? and actually, i changed some code to look like this: while(infile >> holder[i])
{
i++;
} |
|
|
|
|
|
#8 |
|
Unverified User
Join Date: Sep 2005
Posts: 209
Rep Power: 0
![]() |
hey cache,
I still get junk for output, not the actual lines from the file. |
|
|
|
|
|
#9 |
|
Hobbyist
Join Date: Sep 2005
Posts: 266
Rep Power: 4
![]() |
Like I said, that should give you an idea. I don't know what's in your file.
|
|
|
|
|
|
#10 |
|
Unverified User
Join Date: Sep 2005
Posts: 209
Rep Power: 0
![]() |
this is what's in the file:
Employee ID Employee Sex Numeric Wage Years with Company
----------- ------------ ------------ ------------------
6162 F $ 12.23 355
4654654 M $ 23.60 6It easy to get the labels and ----- out and displayed, but I wanted to get the other lines, and get the crap out of em them and store them in separate arrays. But when try to get those lines from a file, it gives me junk. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|