-
Reading Text from a file
im trying to read some text from a file it compiles witn no errors but it doesnt print out whats in the txt file
heres my code
Code:
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
char data [256];
cout<<"opening file..."<<endl;
ifstream open_file ("new.txt", ios::app);
open_file>>data;
cout<< data <<endl;
cin.get();
}
-
ios::app is used when you open a file and write from the end of file.
In your case, you need ios::in
-
ok thanks it works now but ive ran into another problem im trying to use the data i get from the txt file to determine what leval im on but it wont compile
heres my code
Code:
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
char data [256];
cout<<"opening file..."<<endl;
ifstream open_file ("new.txt", ios::in);
open_file>>data;
if(data == 1)
{
cout<<"you are on leval one"<<endl;
}
else if(data == 2)
{
cout<<"you are on leval 2"<<endl;
}
cin.get();
}
-
Your char data[256] is known as C-style string. You compare two of these using the function strcmp() http://cppreference.com/stdstring_details.html#strcmp
And then there's C++-style string declared like this
Code:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string myString = "ABC";
// more intuitive to compare than C-style string
if ( myString == "ABC" )
cout << "Bingo";
return 0;
}
For either string style, it's indicated by opening and closing double quotes (e.g. "H", "123")
BUT, in your case above, you might want to read data from the file to a variable of type int, though.
-
thnx again i noticed it reads text from only the top line what could i do to read it from the whole file or a certain line?
-
I'd like to know that too. How do I make it read in the whole txt document, or a specific line?
Code:
string data;
cout << "opening file..." << endl;
ifstream open_file ("new.txt", ios::in);
while(!open_file.eof())
{
getline(open_file, data);
cout << data << endl;
}
cin.get();
That makes it read the whole document, but I still can't figure out how to pick out a specific line in the document.
-
skipping to a certain point in a document is more of a binary thing, but with text you can always do this:
Code:
...
std::cin.ignore(10000,'\n'); //ignore one line
...
or set up special characters in your files to denote fields, for example, use '$' to denote prices, so you can use this:
Code:
...
std::cin.ignore(10000,'$'); //ignore everything up to + including the next '$'
...
the result of that file would look something like this:
Code:
BanHammer $120.0 ~CB168
DellComputer $50.0 ~DL189
HomeworkThread $0.10 ~CB666
in that file, the first field is the name of the item, then the price, then the catalog number preceded by a '~'
-
just so i get everything
Code:
getline(open_file, data);
is copying open_file to data?
-
>>getline(open_file, data);
using the stream called open_file, remove all data in the input buffer and put it in (ie, read, copy, whatever you want to call the process) the STL string called data until you either run out of memory or until the first newline char is encountered, whichever comes first. If the terminating char terminates input, remove the terminating char from the input buffer but don't put it into the string.