?
Solved

How to read a text/binary file

Posted on 2006-04-12
4
Medium Priority
?
2,902 Views
Last Modified: 2012-08-14
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
Comment
Question by:markov123
  • 2
  • 2
4 Comments
 
LVL 48

Expert Comment

by:AlexFM
ID: 16435802
#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
 
LVL 1

Author Comment

by:markov123
ID: 16436064
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
 
LVL 48

Accepted Solution

by:
AlexFM earned 2000 total points
ID: 16436218
Oops, one more correction:

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

Author Comment

by:markov123
ID: 16436413
Yes, I just managed to figure that out :).

Thanks for your help, everything works fine.

 
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

The following diagram presents a diamond class hierarchy: As depicted, diamond inheritance denotes when two classes (e.g., CDerived1 and CDerived2), separately extending a common base class (e.g., CBase), are sub classed simultaneously by a fourt…
In Easy String Encryption Using CryptoAPI in C++ (http://www.experts-exchange.com/viewArticle.jsp?aid=1193) I described how to encrypt text and recommended that the encrypted text be stored as a series of hexadecimal digits -- because cyphertext may…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…

850 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