Solved

Need help in reading from file.

Posted on 2003-11-13
6
465 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
  • 3
  • 2
6 Comments
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
Comment Utility
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
Comment Utility
You need to alloc memory for list
0
 
LVL 6

Expert Comment

by:GloomyFriar
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:smita_raut
Comment Utility
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
Comment Utility
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
Comment Utility
>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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

763 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now