smita_raut
asked on
Need help in reading from file.
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
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
You need to alloc memory for list
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;
}
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;
}
ASKER
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
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
>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.
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.
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