简体   繁体   English

使用C ++读取和写入文件

[英]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. 我需要使用fscanf读取“test.txt”文件中的每一行,并使用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. 分别浮动到float文件和字符串到字符串文件。 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. feof()永远不会是真的,因为writefloat()中的fscanf拒绝读取“嘿”的第一个字母:它不是合法数字的一部分。 scanf then returns with 0 ("no item could be read"). 然后scanf返回0(“无法读取任何项目”)。 That is not EOF yet, though. 但是,这还不是EOF。 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. 此外,在尝试使用之前,您必须尝试阅读之后检查eof。 Not before the first failed read will the eof flag be turned on, but those variables will not be assigned. 在第一次失败读取之前,不会打开eof标志,但不会分配这些变量。

That is not the common way to open and close a file in C++. 这不是在C ++中打开和关闭文件的常用方法。 It looks like ac program. 它看起来像ac程序。 Try using functions from fstream and iostream libraries. 尝试使用fstream和iostream库中的函数。 See http://www.tutorialspoint.com/cplusplus/cpp_files_streams.htm . 请参阅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". 它行不包含任何数字,该行被写出“string.out.txt”。 If the number is an integer, it is written out to "int.out.txt". 如果数字是整数,则写出“int.out.txt”。 If the number is a floating point number, it is written out to "float.out.txt". 如果该数字是浮点数,则写入“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. 它还简化了数据的读取并检查了何时达到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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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