Solved

Need help in reading from file.

Posted on 2003-11-13
6
478 Views
Last Modified: 2008-02-01
Hi,

I have to read the contents of a text file into a char* variable. I am using visual c++ 6.0 and following is the code snippet-

#include <stdio.h>

static char* readFromFile(const char* fname)
{

   FILE *stream;
   char* list;

   if( (stream = fopen( fname, "r+t" )) != NULL )
   {
      /* Attempt to read */
         fread( list, sizeof( char ), 100000, stream );
         fclose( stream );
   }

   return list;
}

But the fopen fails here. When I tried to debug, it asks me the path of fopen.c.
What am I missing here?

Thanks,
Smita
0
Comment
Question by:smita_raut
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 9738194
if fopen fails you don't need debug fopen.c but look for the error code.

The error you can get from the global variable  errno  that can be evaluated by including <errno.h>.

I suppose that you get errno==2 that is file not found and means that the file path in fname is incorrect.
Other error codes you may check in <winerror.h> where all windows error codes are listed with a comment.

You can easily find winerror.h by typing  #include <winerror.h> somewhere in your code and use context menu to open the document.

Hope, that helps

Alex
0
 
LVL 6

Expert Comment

by:GloomyFriar
ID: 9738622
You need to alloc memory for list
0
 
LVL 6

Expert Comment

by:GloomyFriar
ID: 9738644
Here is working code:

static char* readFromFile(const char* fname)
{

  FILE *stream;
  char* list = NULL;

  stream = fopen(fname, "r+t");

  if (stream != NULL)
  {
     /* Attempt to read */
    list = new char[100000];
    fread(list, sizeof( char ), 100000, stream);
    fclose(stream);
  }

  return list;
}
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:smita_raut
ID: 9739602
Alex,

You were right. I got errno==3, i.e. path not found.
Coz' i had given a relative path like ..\\somefilename

Could you tell me how can we handle relative paths?
I tried _fullpath() to convert it to absolute path, but it gives incorrect result.
e.g. Actual path = C:\dir1\dir2\somefilename
I am at path = C:\dir1\dir2\dir3\
_fullpath() returns = C:\somefilename

I cannot use absolute path.
Can you help?

Thanks,
Smita
0
 
LVL 39

Accepted Solution

by:
itsmeandnobodyelse earned 30 total points
ID: 9740451
Relative path is ok, but you should know where your current directory is.

For VC projects current directory is the project directory if you start your program from Visual Studio.
You may evaluate the current directory path using GetCurrentDirectory() if you can include windows.h or _getcwd()  else.

Note, that all backslash characters explicitly defined must be typed twice within "" , e. g. "C:\\projects\\myfile.dat".
Or you may use forward slashes:  "C:/projects/myfile.dat"

Regards, Alex
0
 
LVL 6

Expert Comment

by:GloomyFriar
ID: 9741894
>Could you tell me how can we handle relative paths?
Usually I inspect the path from which program was started.
And then create full filenames using the start path.

To get the start path use GetModuleFileName.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question