繁体   English   中英

循环打印数组内容时程序挂起

[英]Program Hangs when printing contents of array in loop

嗨,我正在使用MinGW C编译器和Code :: Blocks,尝试打印数组的内容时,我的代码挂起了(这是自定义数据类型)。 快速摘要:程序使用txt文件的内容,并使用称为stringArray的自定义数据类型将字符串拆分为单个单词(名称说明自身)。 然后,它应该将文件的每个单词打印给用户。 问题是,它挂起并显示出通常的“ [PROGRAM NAME HERE]没有响应。” 按取消后,它给我这个结果:

返回的过程-1073741819(0xC0000005)执行时间:3.861 s按任意键继续。

我是一个初学者。

这是代码:

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

typedef struct stringArray
{
   char *string;

}stringArray;

const char delim[2] = " ";

int string_to_array(char *filecontents)
{
    char *token;
    token = strtok(filecontents, delim);

    int i;
    int dirtyContentsLength;
    stringArray newContents[100];

    for(i = 0; i < 100; i++)
    {
        newContents[i].string = "";
    }

    i = 0;

    while (token != NULL)
    {
        newContents[i].string = token;
        i++;
        token = strtok(NULL, delim);
    }

    return newContents;
}

int open_file(char filename[30])
{
    char *file_contents;
    long input_file_size;
    FILE *input_file = fopen(filename, "rb");
    fseek(input_file, 0, SEEK_END);
    input_file_size = ftell(input_file);
    rewind(input_file);
    file_contents = malloc(input_file_size * (sizeof(char)));
    fread(file_contents, sizeof(char), input_file_size, input_file);
    fclose(input_file);

    return file_contents;
}

int lex(char filecontents[30])
{
    char *tok = "";
    int state = 0;
    char *string = "";

}

int main(int argc, char *argv[] )
{
    const char *cleanContents;
    char *messyContents;
    char input[30];
    printf("What is the filename? ");
    scanf("%s", input);
    messyContents = open_file(input);
    cleanContents = string_to_array(messyContents);

    int contentsLength = sizeof(cleanContents) / sizeof(cleanContents[0]);
    int i;
    for(i = 0; i < contentsLength; i++)
    {
        printf("%s\n", cleanContents[i]);
    }

    printf("Done");
    return 0;


}

您的代码有多个问题:

  1. 声明string_to_array()返回一个int ,但实际上它返回一个stringArray
  2. open_file()函数相同,声明返回一个int ,但实际上返回一个char*
  3. string_to_array返回一个在本地声明的元素。 这意味着一旦函数返回,该内存将不再有效,但已将其传递给了调用者。
  4. 您的结构名称具有误导性。 char*是字符数组(字符串)。 因此,名称charArray将更合适。 为了使结构成为字符串数组,它必须是char** ,即字符数组(字符串数组)的数组
  5. 插入main()函数中的printf() ,您不传递字符串(因此会生成编译警告)
  6. 您没有将内存初始化为全0。这是理想选择,因为否则内存将包含随机数据,该数据将被解释为字符串,直到第一个空终止符(遇到\\0为止)

以下代码是您尝试实现的修改后的工作版本,其中包含有关每个更改的注释:

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

typedef struct stringArray
{
   char *string;

}stringArray;

const char delim[2] = " ";

// Now string_to_array takes the memory location to write output to as a first parameter so that the
// memory will reside in the callers scope (refer to problem 3 above)
// Additionally return type was now set to void (refer to problem 1)
void string_to_array(stringArray newContents[100], char *filecontents)
{
    char *token;
    token = strtok(filecontents, delim);

    int i;
    int dirtyContentsLength;

    for(i = 0; i < 100; i++)
    {
        newContents[i].string = "";
    }

    i = 0;

    while (token != NULL)
    {
        newContents[i].string = token;
        i++;
        token = strtok(NULL, delim);
    }

    // return now was removed. result written directly in memory passed as parameter by the caller.
}

// open_file changed to return a char* (refer to problem 2)
char* open_file(char filename[30])
{
    char *file_contents;
    long input_file_size;
    FILE *input_file = fopen(filename, "rb");
    fseek(input_file, 0, SEEK_END);
    input_file_size = ftell(input_file);
    rewind(input_file);
    file_contents = malloc(input_file_size * (sizeof(char)));
    fread(file_contents, sizeof(char), input_file_size, input_file);
    fclose(input_file);

    return file_contents;
}

int lex(char filecontents[30])
{
    char *tok = "";
    int state = 0;
    char *string = "";

}

int main(int argc, char *argv[] )
{
    stringArray cleanContents[100];
    // Initializing memory to all 0s (refer to problem 6)
    memset(cleanContents, 0 ,sizeof(cleanContents));
    char *messyContents;
    char input[30];
    printf("What is the filename? ");
    scanf("%s", input);
    messyContents = open_file(input);
    string_to_array(cleanContents, messyContents);

    int contentsLength = sizeof(cleanContents) / sizeof(cleanContents[0]);
    int i;
    for(i = 0; i < contentsLength; i++)
    {
        // Checking that at least one character is present in the string before printing it...
        if (cleanContents[i].string[0])
        {
            // Printing the string within the 'stringArray'. (refer to problem 5)
            printf("%s\n", cleanContents[i].string);
        }
    }

    printf("Done\n");
    return 0;


}

暂无
暂无

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

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