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

时间:2017-09-03 07:57:13

标签: c arrays

嗨,我正在使用带有Code :: Blocks的MinGW C编译器,当我尝试打印数组的内容时,我的代码会挂起(这是一种自定义数据类型)。 快速摘要:程序正在获取txt文件的内容并进行拆分 使用名为stringArray的自定义数据类型(该名称解释自身)将字符串分成单个单词。然后它应该将文件的每个单词打印给用户。 问题是,它挂起并给了我通常的#34; [此处的程序名称]没有响应。"按下取消后,它给我这个结果:

  

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

我是一个初学者。

以下是代码:

#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 个答案:

答案 0 :(得分:1)

您的代码存在多个问题:

    声明
  1. string_to_array()返回int,但实际上它返回stringArray
  2. open_file()函数相同,声明返回int,但实际返回char*
  3. string_to_array返回一个在本地声明的元素。这意味着一旦返回该函数,该内存不再有效,但它已将其传递给调用者。
  4. 您的结构名称具有误导性。 char*是字符数组(字符串)。因此,名称​​ charArray 会更合适。要使结构成为字符串数组,它必须是char**,即字符数组数组(字符串数组)
  5. printf()函数的main()中,您没有传递字符串(因此会生成编译警告)
  6. 您没有将内存初始化为全0。这是理想的,否则内存将包含随机数据,这些数据将被解释为字符串,直到第一个空终止符(\0遇到)
  7. 以下代码是您尝试通过对每项更改发表评论所实现的修改后的工作版本:

    #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;
    
    
    }