简体   繁体   English

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

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

Hi, i am using the MinGW C Compiler with Code::Blocks and my code hangs when trying to print the contents of an array (well it is a custom data type). 嗨,我正在使用MinGW C编译器和Code :: Blocks,尝试打印数组的内容时,我的代码挂起了(这是自定义数据类型)。 For a quick summary: the program is taking the contents of a txt file and splits the string up into individual words using a custom data type called a stringArray (the name explains itself). 快速摘要:程序使用txt文件的内容,并使用称为stringArray的自定义数据类型将字符串拆分为单个单词(名称说明自身)。 It then should print each word of the file to the user. 然后,它应该将文件的每个单词打印给用户。 The problem is, it hangs and gives me the usual "[PROGRAM NAME HERE] is not responding." 问题是,它挂起并显示出通常的“ [PROGRAM NAME HERE]没有响应。” After pressing cancel it gives me this result: 按取消后,它给我这个结果:

Process returned -1073741819 (0xC0000005) execution time : 3.861 s Press any key to continue. 返回的过程-1073741819(0xC0000005)执行时间:3.861 s按任意键继续。

I am a sort of beginner. 我是一个初学者。

Here is the code: 这是代码:

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


}

You have multiple problems with your code: 您的代码有多个问题:

  1. string_to_array() is declared to return an int , but in reality it is returning a stringArray 声明string_to_array()返回一个int ,但实际上它返回一个stringArray
  2. Same with open_file() function, Declared to return an int , but actually returning a char* open_file()函数相同,声明返回一个int ,但实际上返回一个char*
  3. string_to_array is returning an element that was declared locally. string_to_array返回一个在本地声明的元素。 This means that once the function is returned, that memory is no longer valid, but it has passed it on to the caller. 这意味着一旦函数返回,该内存将不再有效,但已将其传递给了调用者。
  4. Your structure name is misleading. 您的结构名称具有误导性。 A char* is a character array (a string). char*是字符数组(字符串)。 Thus the name charArray would be more appropriate. 因此,名称charArray将更合适。 For the structure to be a string array it has to be a char** , ie an array of character arrays (array of strings) 为了使结构成为字符串数组,它必须是char** ,即字符数组(字符串数组)的数组
  5. Int the printf() in the main() function you are not passing the string (thus a compilation warning is generated) 插入main()函数中的printf() ,您不传递字符串(因此会生成编译警告)
  6. You are not initializing memory to all 0. This is ideal as otherwise the memory will contain random data which will be interpreted as a string untill the first null terminator ( \\0 encountered) 您没有将内存初始化为全0。这是理想选择,因为否则内存将包含随机数据,该数据将被解释为字符串,直到第一个空终止符(遇到\\0为止)

The following code is a modified working version of what you are trying to achieve with comments about each change: 以下代码是您尝试实现的修改后的工作版本,其中包含有关每个更改的注释:

#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