【问题标题】:Stuck on 2D Arrays, inserting a pointer into a string from a file卡在二维数组上,将指针插入文件中的字符串
【发布时间】:2018-06-09 14:26:12
【问题描述】:

编辑:使用普通 c

这部分是指我的最后一个问题,但我现在完全重写了大约 3 次代码,而且我处于一个主要的车辙中。我已经阅读了很多关于 2D 数组的不同内容,我很困惑我应该认为什么是正确的,其他 stackoverflow 帖子让我更加困惑:(

例如:

char array[A][B];

一些消息来源说 A 是 nr。字段和 B 一个字段的长度,而其他人说 A 是 nr。行数和 B nr。矩阵的列。其他人说这只会保存单个字符。

继续我的问题:

我正在写一个测验,我有一个数据库文件,其中每一行如下所示:

Multiple Words A#Multiple Words B#Multiple Words C

现在我想读取文件并将该行拆分为多个变量,这些变量的定义如下:

char frageinhalt[50][255]; // the question itself (later smth like "capital of germany?"
char antw1[50][255]; // the first answer to the question
char antw2[50][255]; // second answ

应该像这样拆分行:

Multiple Words A => frageinhalt
Multiple Words B => antw1
Multiple Words C => antw2

每一行都应该在数组中分配一个字段,所以我可以简单地在其他函数中打印它们。

例如: 我想打印第一个问题及其答案

printf("%s,%s,%s",frageinhalt[0],antw1[0],antw2[0]);

但这在我的代码中不起作用。有什么想法吗?

完整代码如下。

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

int readfromfile(); // func prototype

char data[100]; // a row from the file

char temp[50];

//Fragebezogen
char id[50][5]; // question nr
char frageinhalt[50][255]; // the question itself (later smth like "capital of germany?"
char antw1[50][255]; // the first answer to the question
char antw2[50][255]; // second answ


int main() {
  readfromfile();
  printf("\nFrageinhalt: %s Antw1: %s Antw2: %s\n", frageinhalt[1], antw1[1], antw2[1]); // Doesn't work properly
  return 0;
}
int readfromfile() {
  FILE *datei_ptr;
  int i = 0;
  char ch;
  int lines = 0;
  int k = 0;
  char delimiter[] = ",;#";
  char *ptr;


  datei_ptr = fopen("test.txt", "r");

  if (datei_ptr == NULL) {
    printf("nothing left in file");



 }
  else {

while (!feof(datei_ptr))
{
  ch = fgetc(datei_ptr);
  if (ch == '\n') // Wenn der gerade gelesene Character ein Zeilenumbruch ist..
  {
    lines++; // Erhöhe die Anzahl der Zeilen um 1
  }
}

fclose(datei_ptr);
datei_ptr = fopen("test.txt", "r");
do {
  fgets (data, 255, datei_ptr);
  puts(data);


  ptr = strtok(data, delimiter);
  printf("###############################\n");
  while (ptr != NULL)
  {

    printf("Abschnitt gefunden: %s\n", ptr);

    // naechsten Abschnitt erstellen
    ptr = strtok(NULL, delimiter);
  }
  printf("###############################\n");
  k++;
} while (k != lines + 1);

fclose(datei_ptr);
  }

}

【问题讨论】:

  • readfromfile 函数永远不会在您的数组中写入任何内容。这意味着您的数组将被零初始化(因为它们是全局的)并且零( 0,而不是字符'0')是字符串终止符,它使您的所有字符串为空.
  • 编译时,始终启用警告,然后修复这些警告。 (对于gcc,至少使用:-Wall -Wextra -Wconversion -pedantic -std=gnu11
  • 函数:fgetc() 返回一个int,而不是char
  • 此签名:int readfromfile() 表明该函数返回一个 int,但该函数缺少任何 return &lt;intvalue&gt;

标签: c pointers multidimensional-array file-io


【解决方案1】:

以下建议的代码:

  1. 干净编译
  2. 执行 OP 隐含的功能
  3. 用有意义的名称替换(大部分)“神奇”数字
  4. 消除未使用/不需要的局部变量和不需要的逻辑
  5. 格式化代码以便于阅读和理解
  6. 正确输出错误信息(以及操作系统认为错误发生的原因)到stderr
  7. 为不接收参数的子函数正确声明原型

现在,建议的代码:

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

#define MAX_ROW_LEN      1024
#define MAX_LINES        50
#define MAX_QUESTION_LEN 255
#define MAX_ANSWER_LEN   255

void readfromfile( void ); // func prototype

char data[ MAX_ROW_LEN ]; // a row from the file


//Fragebezogen
char id[ MAX_LINES ][5]; // question nr
char frageinhalt[ MAX_LINES ][ MAX_QUESTION_LEN ]; // the question itself (later smth like "capital of germany?"
char antw1[ MAX_LINES ][ MAX_ANSWER_LEN ]; // the first answer to the question
char antw2[ MAX_LINES ][ MAX_ANSWER_LEN ]; // second answ


int main( void )
{
    readfromfile();
    printf("\nFrageinhalt: %s Antw1: %s Antw2: %s\n",
            frageinhalt[1],
            antw1[1],
            antw2[1]);
    return 0;
}


void readfromfile()
{
    FILE *datei_ptr;
    char delimiter[] = ",;#";
    char *token;

    datei_ptr = fopen("test.txt", "r");

    if ( !datei_ptr )
    {
        perror( "fopen failed" );
        exit( EXIT_FAILURE );
    }

    int lineCounter = 0;
    while( lineCounter < MAX_LINES && fgets (data, sizeof( data ), datei_ptr) )
    {
        puts(data);
        printf("###############################\n");

        token = strtok(data, delimiter);

        if ( token )
        {
            printf("Abschnitt gefunden: %s\n", token);
            strncpy( id[ lineCounter ], token, 5 );

            token = strtok(NULL, delimiter);
            if( token )
            {
                strncpy( frageinhalt[ lineCounter ], token, MAX_QUESTION_LEN );

                token = strtok( NULL, delimiter );
                if( token )
                {
                    strncpy( antw1[ lineCounter ], token, MAX_ANSWER_LEN );

                    token = strtok( NULL, delimiter );
                    if( token )
                    {
                        strncpy( antw2[ lineCounter ], token, MAX_ANSWER_LEN );
                    }
                }
            }
        }

        printf("###############################\n");
        lineCounter++;
    }

    fclose(datei_ptr);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多