【问题标题】:How can I write float data from a .txt file to a 2-dimensional array w/ VS2017?如何将 .txt 文件中的浮点数据写入带有 VS2017 的二维数组?
【发布时间】:2019-01-26 21:18:20
【问题描述】:

如何从 .txt 文件中读取浮点数据:

8.9 789.3 845.6
2.45 2.25 2.05

。 . .并将每个浮点数写入数组元素。我在网上查看过,但找不到有关如何执行此操作的明确答案或教程。我正在使用 VS2017 Enterprise,并且我的项目资源文件中已经有 .txt 文件。

【问题讨论】:

  • 您使用的是 C 还是 C++?
  • 使用strtof 转换您读入的行中的值。

标签: c++ c arrays visual-studio-2017 file-read


【解决方案1】:

这是一个您可以使用的简单示例

#include<stdio.h>
#include<stdlib.h>

int main(){
FILE *f=fopen("file.txt","r");
float a;
char b[255];
while(!feof(f)){
        fscanf(f,"%s",b);
        a=atof(b);
printf("%f\n",a);

} fclose(f);
}

【讨论】:

  • 我宁愿阅读字符串和扫描。它避免了新行等问题。
  • while(!feof(f)){ fscanf(f,"%s",b); 为弱 0。请检查 fscanf() 的返回值。
【解决方案2】:

我总是从解释如何获得更多答案,而不是代码,所以这里是我如何思考这个问题(假设对所选语言知之甚少):

首先,我需要通读一个文件。我现在上网搜索如何读取文件。我找到了几个读取文件的示例,并选择了对我最有意义的内容(无论是将文件流式传输还是将整个文件读入char *)。一旦我弄清楚了,我就可以继续找出二维数组。

要构建二维数组,我首先需要将二维数组初始化为正确的大小。 如果一个人是真正的初学者,那可能意味着要查找如何初始化 2d 数组。 然后我可以继续填充数组。

现在,我遍历二维数组。 如果一个人是真正的初学者,那可能意味着要查找如何迭代二维数组。对于二维数组中的每个单元格,我需要从文件中解析出下一个浮点数并将其插入细胞。

这部分取决于您选择读取文件的方式,可以通过多种方式完成。假设您正在流式传输文件,则需要读取下一个“单词”,然后将该文本转换为浮点数。为此,我可能会搜索诸如“在 C 中读取文本直到空格或换行符”和“将字符串转换为在 C 中浮动”之类的内容。

那么剩下要做的就是测试它!
希望对您有所帮助。

【讨论】:

    【解决方案3】:

    使用system.IO读取文件中的文本,然后使用C# split转换为数组。

    命名空间

    using System;
    using System.IO;
    using System.Text;
    

    在您的代码中

    string path = @"c:\temp\MyTest.txt";
    // Open the file to read from.
    string readText = File.ReadAllText(path);
    string[] strarray = readText.Split(Convert.ToChar(' '));
    

    如果要将其转换为double 数组。使用linq

    double[] darray = readText.Split(Convert.ToChar(' ')).Select(n => Convert.ToDouble(n)).ToArray();
    

    【讨论】:

    • OP 标记为 C 和 C++,而不是 C#
    【解决方案4】:

    将数据从 .txt 文件浮动到二维数组

    找不到有关如何执行此操作的明确答案或教程。

    伪代码大纲

    制作这些函数

    // return 1: success
    // return 0: failure
    // return -1: EOF or end of line
    int read_one_float(FILE *stream, float *f) 
      fgetc(): read leading white space, if \n encountered, return -1
      ungetc(): put non white space character back in stream
      if f == NULL, form a dummy location to save data
      scanf("%f", f) and return its value
    
    // return > 0: return count of float
    // return 0: failure
    // return -1: EOF
    int read_one_line(FILE *stream, float *f) 
      count = 0
      repeatedly 
        call read_one_float(f)
        if (response != 1) return count
        if (f not NULL) f++
        count++;
      return count
    
    // return > 0: return count of lines
    // return 0: failure
    // return -1: EOF
    int read_FILE(FILE *stream, float **f, int *width)  
      line = 0;
      *width = 0
      repeatedly 
        call read_one_line(f)
        if (response <= 0) return line
        *width = max (*width, response)
        if (f not NULL) f++
        line++;
      return line 
    

    放在一起

    int main()  
      open file
      width = 0
      line count = read_FILE(file, NULL, &width);
      allocate f float[count][width]
      REWIND file
      read_FILE(file, f, &width);
      close file
    
      do something with f
    
      deallocate
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-06
      • 1970-01-01
      • 1970-01-01
      • 2012-09-17
      相关资源
      最近更新 更多