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

int main()
{
int a = 0, len1 = 0, len2 = 0;
int BUFSIZE = 1000;
char *string1[1];
char *string2[1];
FILE *fp1 = fopen("input1.txt", "r");
FILE *fp2 = fopen("input2.txt", "r");
if ((fp1 == 0)||(fp2 == 0))
{
    fprintf(stderr, "Error while opening");
    return 0;
}
string1[a] = (char *)malloc(BUFSIZE);
string2[a] = (char *)malloc(BUFSIZE);
fgets(string1[0], BUFSIZE, fp1);
fgets(string2[0], BUFSIZE, fp2);
len1=strlen(string1[0]);
len2=strlen(string2[0]);

printf("%c\n", string1[0][4]);
printf("Output: \n");
srand(time(NULL));
printf("%s\n", string1[0]);
printf("%s\n", string2[0]);
printf("\n");
printf("%d %d", len1, len2); 
printf("\n");

free(string1[0]);
free(string2[0]);
int x=0;
scanf("%d", &x);
fclose(fp1);
fclose(fp2);
return 0;
}

我需要从文件中读取一个字符串并将其存储在一个数组中。我只需要读取一行字符串,数组的每个元素都应该是字符串的一个字符。例如,如果我将“ABCDABC”读入一个数组,那么 array[3] 应该是“D”。但是我真的不知道怎么做,我修改了一些别人的代码,得到了上面的代码。但我不想在我的代码中涉及指针和地址的东西。那么谁能告诉我如何在不使用指针的情况下实现它?谢谢!

4

2 回答 2

2

代替char *and malloced 内存,你也可以使用 plain char[N]。更改很小,因为 achar[N]在传递给函数时会自动转换为指向其第一个元素的指针(likjefgetsstrlen)。char*如果您使用普通forstring1string2,代码也只会发生很小的变化char *string1 = malloc(BUFSIZE);,这里的主要区别是最后的mallocfree

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

int main()
{
    int a = 0, len1 = 0, len2 = 0;
    int BUFSIZE = 1000;

    // char *string1;
    // char *string2;

    // instead of one-element arrays of char*, let's just use char arrays

    char string1[BUFSIZE];
    char string2[BUFSIZE];

    // One pointer to check whether fgets succeeded
    char *suc;

    FILE *fp1 = fopen("input1.txt", "r");
    FILE *fp2 = fopen("input2.txt", "r");
    if ((fp1 == 0)||(fp2 == 0))
    {
        fprintf(stderr, "Error while opening");
        return 0;
    }
    // No need for malloc, we have char arrays
    // string1 = (char *)malloc(BUFSIZE);
    // string2 = (char *)malloc(BUFSIZE);
    suc = fgets(string1, BUFSIZE, fp1);
    if (!suc) {
        // fgets failed, what now? exit?
        return EXIT_FAILURE;
    }
    suc = fgets(string2, BUFSIZE, fp2);
    if (!suc) {
        // see above
        return EXIT_FAILURE;
    }
    len1=strlen(string1);
    len2=strlen(string2);

    // is the read string long enough?
    if (len1 > 4) {
        printf("%c\n", string1[4]);
    }
    printf("Output: \n");
    srand(time(NULL));
    printf("%s\n", string1);
    printf("%s\n", string2);
    printf("\n");
    printf("%d %d", len1, len2); 
    printf("\n");

    // free is only for m/c/re-alloced memory
    // free(string1);
    // free(string2);
    int x=0;
    scanf("%d", &x);
    fclose(fp1);
    fclose(fp2);
    return 0;
}
于 2012-11-10T22:24:42.947 回答
0

要只读取一行,您必须检查字符'\n',您必须这样做

char *readLine(char *fileName, int line)
{
    FILE *file = fopen(fileName, "r");
    char *code;
    size_t n,l;
    int c;

    if (file == NULL)
        return NULL; //could not open file

    code = malloc(1000);

    for(l = 0; l <= line; l++)
    {
        n = 0;
        while (c = fgetc(file)) != '\n')
        {
            code[n++] = (char) c;
        }
    }

    // don't forget to terminate with the null character
    code[n] = '\0';        

    return code;
}

此代码将文档的 x 行字符串逐个字符地读取文件,直到找到换行符 x 次。char *string1 = readLine("C:\document.txt",1); 如果第二行是"Sometext"那么你写这样的东西string[3] == 'e'

于 2012-11-10T22:23:48.140 回答