Reading and writing to a file

Posted on 2006-04-15
Last Modified: 2010-04-24
I have written a program to read from and write to a text file. AM doing something wrong in the code. Please check it and let me know with some comments.

#include "stdafx.h"

int _tmain(int argc, _TCHAR* argv[])
FILE  *infile;
FILE  *outfile;

char outchars[200];
char inchars[200];

infile = fopen("c:\\nasser\\cprograms\\tin.txt","r");

if (infile == NULL)
      printf("Cannot find input file");

outfile = fopen("c:\\nasser\\cprograms\\tout.txt","w");

if (outfile == NULL)
      printf("Cannot find output file");

while (fgets(inchars, 200, infile) !=NULL
fputs(outchars,  outfile);


      return 0;

Thanks in advance

Mark Markov
Question by:markov123
    LVL 4

    Expert Comment

    >while (fgets(inchars, 200, infile) !=NULL
    >fputs(outchars,  outfile);

    what do you want to do with this two lines of code?

    there is a ')' missing after '!=NULL'
    LVL 1

    Author Comment

    Actually, I would like to process the file and then write to another file.

    instead uf using fgets, would like to use fscanf to count the number of characters.

    I have:

    while (fscanf(infile, inchars) != NULL)
           acount = acount + 1;

           fputs(inchars, outfile);

    Basically counting characters and writing to another file, but somehow looks like I got the fscanf syntax wrong.

    Help is appreciated.

    LVL 4

    Assisted Solution

    fscanf will read data from the stream and stores it into the locations given by argument(s)

    to read/write character by character: -> fscanf will ignore all whitespaces (space, newline, tab)

      char c[2] = {0};
      FILE * rFile;
      FILE * wFile;
      rFile = fopen ("readfile.txt","r");
      wFile = fopen ("writefile.txt","w+");
      int count = 0;
      while(fscanf (rFile, "%c", c) != EOF) // will read one char from file
      fclose (rFile);
      fclose (wFile);

    C++ filestream would be a better way:

      std::ofstream ofs;
      std::ifstream ifs;"writefile.txt");"readfile.txt");

      int count = 0;

    LVL 3

    Accepted Solution

    chip3d's solution should work.  Be aware that reading one character at a time will be (relatively) slow for a large file.  If you read it in blocks, it'll be faster.  Something like this for the main loop:

    int characterCount = 0;
    while (!feof(rFile))
      const int kBufferSize = 1024; // read up to this many bytes at a time.
      char buffer[kBufferSize];
      int numRead = fread(buffer, 1, kBufferSize, rFile); // read from rFile into buffer.  Returns the number of items read
      characterCount += numRead;
      fwrite(buffer, 1, numRead, wFile); // write the data read in, out to wFile

    If all you want to do is find the length of the file, and copy it to another location, you could have your program call functions that specifically do that.  If you're programming on Windows, there are "CopyFile" and "GetFileSize" functions as part of the Windows API.

    Featured Post

    Maximize Your Threat Intelligence Reporting

    Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

    Join & Write a Comment

    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++ ( I described how to encrypt text and recommended that the encrypted text be stored as a series of hexadecimal digits -- because cyphertext may…
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…
    This video discusses moving either the default database or any database to a new volume.

    754 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

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now