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
Who is Participating?

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

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.

>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;"writefile.txt");"readfile.txt");

  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.

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
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.