File Read Issue

V

Venkat

Hi,

I am buried with the following issue on Linux platform, please let me know
if someone can help me out.

I am using ifstream to open and read the file, even i am able to open the
file i am NOT able to read the file contents.
The code i used is below.

fp = fopen(logFile, "a");
ifstream inFile;
inFile.open("/usr/local/cm/idp/test.txt");
if(!inFile.is_open())
{
fprintf(fp, "\n Unable to open the file");
}
else
{
fprintf(fp, "\n I can open the file");
}

std::string line;
getline (inFile, line);
fprintf("\nHi the Line is %s", line.c_str());
inFile.close();
fclose(fp);

It prints the following in my log file
I can open the file
Hi the Line is

The target file i.e. /usr/local/cm/idp/test.txt has all the permissions i.e.
777
Believe me the test.txt is not empty.
It used to work before but than it stopped working.

regards,
Venkat
 
V

Victor Bazarov

Venkat said:
Hi,

I am buried with the following issue on Linux platform, please let me know
if someone can help me out.

I am using ifstream to open and read the file, even i am able to open the
file i am NOT able to read the file contents.
The code i used is below.

fp = fopen(logFile, "a");
ifstream inFile;
inFile.open("/usr/local/cm/idp/test.txt");
if(!inFile.is_open())
{
fprintf(fp, "\n Unable to open the file");
}
else
{
fprintf(fp, "\n I can open the file");
}

std::string line;
getline (inFile, line);

You never test the state of the 'inFile' here. Why?
fprintf("\nHi the Line is %s", line.c_str());

Don't you mean

fprintf(fp, "\nHi the Line is %s", line.c_str());

? Which prompts me to ask you to post _real_ _complete_ code _and_
the file you're trying to read. Please do not expect us to test your
program reading file in /usr/local/... Presume the file is in the local
directory and use

ifstream inFile("test.txt");
inFile.close();
fclose(fp);

It prints the following in my log file
I can open the file

There should be an extra space before 'I'. Is there?
Hi the Line is

The target file i.e. /usr/local/cm/idp/test.txt has all the permissions i.e.
777
Believe me the test.txt is not empty.
It used to work before but than it stopped working.

What's changed?

Anyway, let's start over. Create a very simple program that opens your
file and attempts to read it. If it works, go back to your big program
and figure out what is different between them. If the very simple program
doesn't work, post it here. Completely. And the test file too. No
attachments, please.

V
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top