简体   繁体   中英

Reading and Writing to a File using C++

I need to read each line in the "test.txt" file using fscanf and print to new files using fprintf. If the read line is an integer, it is written to the integer file.

Float to the float file and string to string file respectively. However, when i try to compile it and run it, nothing happens and it goes to infinite loop.

Here is my code

#include <iostream>
#include <stdio.h>

using namespace std;

void writeFloat(){

  FILE *file;
  FILE *file2;
  float value;

  file = fopen("test.txt", "r");
  file2 = fopen("float.out.txt", "w");

  while(!feof(file)){

    fscanf(file, "%f", &value);
    fprintf(file2,"%f", value);

  }

  fclose(file);
  fclose(file2);

}

void writeInteger(){

  FILE *file;
  FILE *file2;
  int value;

  file = fopen("test.txt", "r");
  file2 = fopen("int.out.txt", "w");

  while(!feof(file)){

    fscanf(file, "%d", &value);
    fprintf(file2, "%d", value);
  }

  fclose(file);
  fclose(file);
}

void writeString(){
  FILE *file;
  FILE *file2;
  char value;

  file = fopen("test.txt", "r");
  file2 = fopen("string.out.txt", "w");

  while(!feof(file)){

    fscanf(file, "%s", &value);
    cout<<value<<endl;
    fprintf(file2, "%s", value);

  }

  fclose(file);
  fclose(file2);
}

int main(){

  writeFloat();
  writeInteger();
  writeString();

  return(0);

}



The test.txt file contains the values:

100
1.6E-10
hey nice to meet you.
43
56
4.5E-09
what is going on?

I don't know what wrong with my code. Please help me to achieve my requirement.

feof() is never true because fscanf in writefloat() refuses to read the first letter of "hey": It's not part of a legal number. scanf then returns with 0 ("no item could be read"). That is not EOF yet, though. But you should do something about it ;-).

Besides you must check for eof after you try to read something, before you try to use it. Not before the first failed read will the eof flag be turned on, but those variables will not be assigned.

That is not the common way to open and close a file in C++. It looks like ac program. Try using functions from fstream and iostream libraries. See http://www.tutorialspoint.com/cplusplus/cpp_files_streams.htm .

I think, adopting a different strategy might be more appropriate for your needs.

Just have one function that reads the contents of the file line by line. It checks whether the line contains an integer or a floating point number. It line does not contain any numbers, the line is written out to "string.out.txt". If the number is an integer, it is written out to "int.out.txt". If the number is a floating point number, it is written out to "float.out.txt".

With this strategy, you have to read the contents of the input file only once and process the contents of the file only once.

It also simplifies the reading of the data and checking when you have reached EOF.

#include <stdio.h>

void writeData()
{
  FILE *file1 = NULL;
  FILE *file2 = NULL;
  FILE *file3 = NULL;
  FILE *file4 = NULL;
  char value;
  double realNum = 0.0;
  int intNum = 0;
  int n = 0;
  char line[256];

  file1 = fopen("test.txt", "r");
  file2 = fopen("string.out.txt", "w");
  file3 = fopen("int.out.txt", "w");
  file4 = fopen("float.out.txt", "w");

  while ( fgets(line, 255, file1) != NULL )
  {
     // Each line can be plain old text, a floating point number, or an
     // integer.

     // If the line does not contain a number, assume it is a float.
     // Try to read a real number.
     n = sscanf(line, "%lf", &realNum);
     if ( n == 0 )
     {
        // The line does not have a number.
        // Write the line to the text file.
        fputs(line, file2);
     }
     else
     {
        // We have a real number.
        // Could it be just an integer?
        // Read the integer.
        sscanf(line, "%d", &intNum);

        // How do we decide whether the number is a real number or an
        // integer?
        // Is 1.0 a real number or an integer?
        // Assume for now it is an integer.
        if ( realNum == intNum )
        {
           // We have an integer.
           fprintf(file3, "%d\n", intNum);
        }
        else
        {
           // We have a real number.
           fprintf(file4, "%lG\n", realNum);
        }
     }
  }

  fclose(file4);
  fclose(file3);
  fclose(file2);
  fclose(file1);
}

int main()
{
  writeData();
  return(0);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM