Jump to content

[Urgent] Program does not read or write to a file

So this is pretty urgent, I would love to get help asap.

 

EDIT: Forgot to mention this is C++.

 

I have a code, it works with cin and cout but when I replace it with reading / writing to file it simply does not work, no syntax errors or nothing.

Both files are in the same directory as source.

 

sorry for variable names, it is in latvian

 

Code here: http://pastebin.com/K2wHz4Kn

 

Please ignore the first functions, I know they might be wrong.

 

 

Link to post
Share on other sites

I don't know c++ that well, but my guess is that you are not reading.

 

The open function just does that opens the file. you would need to perfom a read function on the open file.

 

I think this would do the trick 

 

Get line (public member function )

Link to post
Share on other sites


#include <iostream>

#include <fstream>

using namespace std;

int main() {

ifstream myfile ("myfile.txt");

String line;

if (myfile.is_open()) {

while (getline(myfile, line)) {

// line of file is now in varible line, do something with it

}

myfile.close();

} else {

// Panic, the file was not readable

return 1;

}

return 0;

}

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×