简体   繁体   English

打印数组中的字符串时出现奇怪的字符?

[英]Weird characters when printing out strings in an array?

I'm currently working on a program that reads strings from a file and stores them into a 2-D array. 我目前正在开发一个程序,该程序从文件中读取字符串并将其存储到二维数组中。 However, when I try to print out the contents of the array, I get a random character every time. 但是,当我尝试打印出数组的内容时,每次都会得到一个随机字符。 Here is my code: 这是我的代码:

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

int main(){
    FILE* file_pointer;
    char user_input[80];
    char line[81];
    char all_lines_array[100][81];
    int total_lines = 0;
    while (1){
        printf("Please enter a command: ");
        scanf("%s", &user_input);
        if (strstr(user_input, "read") != NULL){
            file_pointer = fopen("C:\\Users\\Tyler\\Desktop\\Hello.txt","r");
            while (fgets(line, 100, file_pointer)) {
                line[strlen(line)+1] = "\0";
                *all_lines_array[total_lines] = line; //My guess is this is wrong
                total_lines++;
        }
        fclose(file_pointer);
    }
}
return 0;
}

I suspect that this is because I'm incorrectly inserting string into my 2-D array, but I have no idea what it is I'm doing wrong. 我怀疑这是因为我不正确地将字符串插入到二维数组中,但是我不知道我做错了什么。 I've set the numbers so that there can only be a maximum of 100 lines in a file, and each line can only be 80 characters long (with the "\\0" at the end). 我已经设置了数字,以便文件中最多只能有100行,并且每行只能有80个字符长(末尾带有"\\0" )。

Here is my input file: 这是我的输入文件:

John Doe    1221 Washington St.    1234567
Jane Doe    1233 Washington St.    1234568
Cain Doe    1234 Washington St.    1234569

There where some statements flagged when compiling with -Wall [which I always recommend doing], which may have helped with some of the errors. 在使用-Wall进行编译时,有些地方标记了一些语句(我总是建议这样做),这可能有助于解决某些错误。

Here's the corrected version, annotated with comments [please pardon the gratuitous style cleanup]: 这是经过更正的版本,带有注释[请原谅免费的样式清理]:

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

int
main()
{
    FILE *file_pointer;
    char user_input[80];
    char line[81];
    char all_lines_array[100][81];
    int total_lines = 0;

    while (1) {
        printf("Please enter a command: ");

        // compiler flagged this with a warning
#if 0
        scanf("%s", &user_input);
#else
        scanf("%s", user_input);
#endif

        if (strstr(user_input, "read") != NULL) {
#ifndef CRAIG
            file_pointer = fopen("C:\\Users\\Tyler\\Desktop\\Hello.txt", "r");
#else
            file_pointer = fopen("input.txt", "r");
#endif
            if (file_pointer == NULL) {
                printf("file not found\n");
                continue;
            }

            // NOTE: using sizeof here is better as 100 was specified but
            // "line" was only 81
            while (fgets(line, sizeof(line), file_pointer)) {
                // NOTE: I presume this is to strip the newline
                // in the original form, it would add garbage chars to the end
                // [because of "\0" instead of '\0']
#if 0
                line[strlen(line) + 1] = "\0";
#else
                line[strlen(line) - 1] = 0;
#endif

                // NOTE: the compiler flagged this as well
#if 0
                *all_lines_array[total_lines] = line;   // My guess is this is wrong
#else
                strcpy(all_lines_array[total_lines],line);
#endif
                total_lines++;
            }

            fclose(file_pointer);
        }
    }

    return 0;
}

fgets is reading 100 chars but your static allocated variable char line[81] is only allocates memory for 81 chars. fgets读取100个字符,但是静态分配的变量char line[81]仅分配81个字符的内存。 instead of the = operator use strcpy 代替=运算符,使用strcpy

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

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