By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,433 Members | 771 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,433 IT Pros & Developers. It's quick & easy.

File Read Issue

P: n/a
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
Aug 18 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Venkat wrote:
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
Aug 18 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.