Solved

Need help in reading from file.

Posted on 2003-11-13
6
470 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
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.

911 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

20 Experts available now in Live!

Get 1:1 Help Now