繁体   English   中英

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

[英]Reading and Writing to a File using C++

我需要使用fscanf读取“test.txt”文件中的每一行,并使用fprintf打印到新文件。 如果读取的行是整数,则将其写入整数文件。

分别浮动到float文件和字符串到字符串文件。 但是,当我尝试编译并运行它时,没有任何反应,它会进入无限循环。

这是我的代码

#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?

我不知道我的代码有什么问题。 请帮我实现我的要求。

feof()永远不会是真的,因为writefloat()中的fscanf拒绝读取“嘿”的第一个字母:它不是合法数字的一部分。 然后scanf返回0(“无法读取任何项目”)。 但是,这还不是EOF。 但是你应该对此做些什么;-)。

此外,在尝试使用之前,您必须尝试阅读之后检查eof。 在第一次失败读取之前,不会打开eof标志,但不会分配这些变量。

这不是在C ++中打开和关闭文件的常用方法。 它看起来像ac程序。 尝试使用fstream和iostream库中的函数。 请参阅http://www.tutorialspoint.com/cplusplus/cpp_files_streams.htm

我认为,采用不同的策略可能更适合您的需求。

只需要一个函数来逐行读取文件的内容。 它检查该行是包含整数还是浮点数。 它行不包含任何数字,该行被写出“string.out.txt”。 如果数字是整数,则写出“int.out.txt”。 如果该数字是浮点数,则写入“float.out.txt”。

使用此策略,您只需读取输入文件的内容一次,并仅处理文件的内容一次。

它还简化了数据的读取并检查了何时达到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