Link to home
Start Free TrialLog in
Avatar of markov123
markov123

asked on

How to read a text/binary file

I am quite new to visual studio IDE and .net.

I am writing a program in C using Visual Studio 2005 environment.

The program simply reads from a text file and prints out.

Here is the syntax:

-------------------------------------------- snip --------------------------

#include "stdafx.h"


int _tmain(int argc, _TCHAR* argv[])
{
      FILE *myfile;
      char buffer[256];

      myfile = fopen("c:\testread.txt","rt");
      
      if myfile != NULL
      {
            while fgets(buffer, 256, myfile) !=NULL)
                  printf("%s", buffer);
            fclose(myfile);
      }
      return 0;
}

{The top 2 lines were added by the compiler during compilation.}


------ Rebuild All started: Project: filetest, Configuration: Debug Win32 ------
Deleting intermediate and output files for project 'filetest', configuration 'Debug|Win32'
Compiling...
stdafx.cpp
Compiling...
filetest.cpp
c:\markov\cprograms\filetest\filetest\filetest.cpp(12) : warning C4996: 'fopen' was declared deprecated
        c:\program files\microsoft visual studio 8\vc\include\stdio.h(234) : see declaration of 'fopen'
        Message: 'This function or variable may be unsafe. Consider using fopen_s instead. To disable deprecation, use _CRT_SECURE_NO_DEPRECATE. See online help for details.'
c:\markov\cprograms\filetest\filetest\filetest.cpp(14) : error C2061: syntax error : identifier 'myfile'
c:\markov\cprograms\filetest\filetest\filetest.cpp(15) : error C2143: syntax error : missing ';' before '{'
c:\markov\cprograms\filetest\filetest\filetest.cpp(16) : error C2061: syntax error : identifier 'fgets'
c:\markov\cprograms\filetest\filetest\filetest.cpp(16) : error C2059: syntax error : ')'
Build log was saved at "file://c:\markov\CPrograms\filetest\filetest\Debug\BuildLog.htm"
filetest - 4 error(s), 1 warning(s)
========== Rebuild All: 0 succeeded, 1 failed, 0 skipped ==========


The reason I wrote it in C is bec I am not familiar with C++.

I would like the gurus to help me out in

1. Fixing the C code
2. Rewriting in C++.net


Thanking you

Mark
Avatar of AlexFM
AlexFM

#include "stdafx.h"


int _tmain(int argc, _TCHAR* argv[])
{
    FILE *myfile;
    char buffer[256];

    errno_t e = fopen_s(&myfile, "c:\testread.txt", "rt");

    if ( e != 0 )
    {
        printf("Error opening file");
        return 0;
    }

    // ??
    while ( fgets(buffer, 256, myfile) !=NULL )
    {
        printf("%s", buffer);
    }

    fclose(myfile);

    return 0;
}

This is compiled successfully, but will not work for binary file. You cannot read inary file using string operations. Replace fgets with _fgetchar for binary files. I think for text files this is OK.
Avatar of markov123

ASKER

Thanks, compiles ok, but I cannot seen to read the file. I made sure the file is in the right directory and the right filename and everything else.

Sorry for sounding so stupid, but I do get to my error message which says file cannot be read. Where have I gone wrong

Thanks in advance.

Mark
ASKER CERTIFIED SOLUTION
Avatar of AlexFM
AlexFM

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Yes, I just managed to figure that out :).

Thanks for your help, everything works fine.