Solved

Need help in reading from file.

Posted on 2003-11-13
6
474 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
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 

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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
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 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…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

803 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