Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Need help in reading from file.

Posted on 2003-11-13
6
Medium Priority
?
492 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
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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 90 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
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 concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

926 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