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

Reading and writing to a file

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
  • 2
2 Solutions
>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'
markov123Author Commented:
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.

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;


  int count = 0;

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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

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