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
LVL 1
markov123Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

AlexFMCommented:
#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.
markov123Author Commented:
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
AlexFMCommented:
Oops, one more correction:

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
markov123Author Commented:
Yes, I just managed to figure that out :).

Thanks for your help, everything works fine.

 
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual C++.NET

From novice to tech pro — start learning today.