Click here to Skip to main content
15,895,667 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi
I'm trying to write/read from a file in c++. The function which writes to a file is as follows:

C++
const char * EncQR = Extract_ClientPublicKey_Encrypt_QRcode(str, ClientCert_String);

ofstream myfile1;
myfile1.open("ram.txt");
if (myfile1.is_open())
  {
	   myfile1 <<EncQR<<"\n";
  }
else
   cout<<"Unable to open file";


The previous code works fine, and write into a txt file. But the problem is with the next function, which tries to read from a file:

C++
int _tmain(int argc, _TCHAR* argv[])
{
   string line;
  // reading a text file
  ifstream myfile1("ram.txt");
  if (myfile1.is_open())
  {
   while(! myfile1.eof()) {
			getline(myfile1, line);
			std::cout << line << "\n\n";}
  }

  else cout << "Unable to open file";
  getch();
  myfile1.close();
  return 0;
}

Where i keep getting "Unable to open file". I've tried another file consisting of a single line, and it was working. but with the "ram.txt" file, which consists of multiple lines, it doesn't work.
I even tried to copy the file into the directory in which my project is located, and still, didnt work.

Need help pleeeeease

Regards.
Posted
Comments
CHill60 23-Mar-14 14:50pm    
Have you closed the file after writing to it?
raniam 23-Mar-14 15:04pm    
hiyes, i just did, but still the same problem exists "unable to open file"

const char * EncQR = Extract_ClientPublicKey_Encrypt_QRcode(str, ClientCert_String);

ofstream myfile1;
myfile1.open("ram.txt");
if (myfile1.is_open())
{
myfile1 <<EncQR<<"\n";
myfile1.close();
}
else
cout<<"Unable to open file";

1 solution

Where is the file being created?

You might try:

open(".\\ram.txt")

for both functions. I think you'll find it works.

Matt
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900