从文本文件读取数字到2D数组

时间:2015-05-05 22:12:50

标签: c

尽管这里有很多例子我似乎无法让这个工作......

我有一个包含许多行的文本文件,每行有三个(int)值,用一个空格分隔。例如:

lines[0][0] = 1
lines[0][1] = 0
lines[0][2] = 0
and so on.....

我试图把它读成2d数组。

到目前为止我的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME1 = @"c:\temp\test.xml";
        const string FILENAME2 = @"c:\temp\test2.xml";
        static void Main(string[] args)
        {
            DataSet ds1 = new DataSet();
            ds1.ReadXml(FILENAME1);
            DataSet ds2 = new DataSet();
            ds2.ReadXml(FILENAME2);

            var results = from dataRows1 in ds1.Tables["member"].AsEnumerable()
                                     join dataRows2 in ds2.Tables["member"].AsEnumerable()
                                     on dataRows1.Field<string>("id") equals dataRows2.Field<string>("id") into ps
                                     select dataRows1;
            DataTable combinedTable = results.CopyToDataTable();

        }
    }
}​

有人可以提供建议我如何进一步开发这个,以便行中的每个值都存储在一个单独的数组索引中?在上面的例子中,我们会存储类似的内容:

// Setup configuration sources.
var configuration = new Configuration()
    .AddJsonFile("config.json")
    .AddJsonFile($"config.{env.EnvironmentName}.json", optional: true);

if (env.IsEnvironment("Development"))
{
    // This reads the configuration keys from the secret store.
    // For more details on using the user secret store see http://go.microsoft.com/fwlink/?LinkID=532709
    configuration.AddUserSecrets();
}
configuration.AddEnvironmentVariables();
Configuration = configuration;

目前它将整行存储在一个数组pos。

你可能会说我是一个C菜鸟,任何帮助都会很棒!

3 个答案:

答案 0 :(得分:3)

使用#include <stdio.h> int main(int argc, char *argv[]) { int i, j; int lines[18][3]; i = 0; while ( i != sizeof(lines) / sizeof(lines[0]) && 3 == scanf("%i %i %i", lines[i] + 0, lines[i] + 1, lines[i] + 2) ) { i++; } for (j = 0; j !=i; j++) { printf("%i %i %i\n", lines[j][0], lines[j][1], lines[j][2]); } return 0; } 。例如:

fscanf

请注意,输入是从标准输入读取的(使用./a.out < data.txt以获得更大的灵活性),这意味着必须将上面的代码段调用为class myClass { var test: Int? static func testFunc() { var array = [myClass] (count: 30, repeatedValue: myClass()) for i in 0...20 { array[i].test = i*2 } for a in 0...20 { println(array[a].test) } } }

答案 1 :(得分:3)

你很顺利,你只是在思考你正在阅读一个字符数组而不是一组有符号字符(可以改为int等)时遇到了问题。这是你的例子:

#include <stdio.h>

#define MAXB 32
#define MAXL 18
#define MAXD 3

int main(void)
{
    int i = 0;
    int numlines = 0;
    char buf[MAXB] = {0};
    char lines[MAXL][MAXD];

    FILE *fp = fopen("inputs/control.txt", "r");

    if (fp == 0)
    {
       fprintf(stderr, "failed to open inputs/control.txt\n");
       return 1;
    }

    while (i < MAXL && fgets (buf, MAXB - 1, fp))
    {
        if (sscanf (buf, "%hhd %hhd %hhd", &lines[i][0], &lines[i][1], &lines[i][2]) == 3)
            i++;
    }

    fclose(fp);

    numlines = i;
    int j = 0;

    for (i = 0; i < numlines; i++)
        for (j = 0; j < MAXD; j++)
            printf (" line[%2d][%2d] : %hhd\n", i, j, lines[i][j]);

    printf ("\n");

    return 0;
}

<强>输出

$ ./bin/read_array_a3
 line[ 0][ 0] : 1
 line[ 0][ 1] : 0
 line[ 0][ 2] : 0
 line[ 1][ 0] : 0
 line[ 1][ 1] : 0
 line[ 1][ 2] : 0
 line[ 2][ 0] : 1
 line[ 2][ 1] : 0
 line[ 2][ 2] : 1
 line[ 3][ 0] : 0
 line[ 3][ 1] : 0
 line[ 3][ 2] : 2
 line[ 4][ 0] : 1
 line[ 4][ 1] : 0
 line[ 4][ 2] : 2

注意: char lines[MAXL][MAXD];很好,您必须明白每个元素都限制为 8位有符号值,这意味着{{1}之间的值}}。如果您需要存储更大的值,可以将它们设为-128 < val < 127

答案 2 :(得分:2)

使用fgets,您正在从文件中读取整行。然后,您需要解析此行以提取单个数字并将其存储到数组中。在while循环中,您可以读取缓冲区中的行,然后使用类似strtok / sscanf的内容来解析每个数字。

相关问题