• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2909
  • Last Modified:

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
0
markov123
Asked:
markov123
  • 2
  • 2
1 Solution
 
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.
0
 
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
0
 
AlexFMCommented:
Oops, one more correction:

errno_t e = fopen_s(&myfile, "c:\\testread.txt", "rt");
0
 
markov123Author Commented:
Yes, I just managed to figure that out :).

Thanks for your help, everything works fine.

 
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now