为什么指针值会改变?

时间:2017-03-12 10:00:02

标签: c

这是我的代码,我想读取frm txt文件并将字符串传递给print函数来处理它打印它:

 #include<stdio.h>
    char *readfile(FILE *fp);
    void printstring(char *inputString);
    int main()
    {
        FILE *filePointer;
        filePointer=fopen("input.txt","r");
        char *inputString=readfile(filePointer);
        printstring(inputString);

    }
    void printstring(char *inputString){
        int i=0;
        //char *ch[]=inputString;
        while((*(inputString+i))!='\0'){
            char c=(*(inputString+i));
            printf("%c",c);
            i++;
        }

    }


    char *readfile(FILE *fp){

        char c;
        int count;
        while((c=getc(fp))!=EOF){
        //printf("%c",c);
        count++;
        }
        char string[count+1];

        int i=0;
        rewind(fp);
        while((c=getc(fp))!=EOF){
        string[i]=c;
        i++;
        }
        string[i+1]='\0';
        char *chptr= &string[0];
        return chptr;
    }

输入文件内容:

12345

1234567

输出:

nazi@nazi-laptop:~$ gcc -o rab RabinKrap.c -g
nazi@nazi-laptop:~$ ./rab
1�+nazi@nazi-laptop:~$

在while循环中赋值之后,将其重新初始化为其他内容。

nazi@nazi-laptop:~$ gdb ./rab
GNU gdb (Ubuntu/Linaro 7.4-2012.04-0ubuntu2.1) 7.4-2012.04
Copyright (C) 2012 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "i686-linux-gnu".
For bug reporting instructions, please see:
<http://bugs.launchpad.net/gdb-linaro/>...
Reading symbols from /home/nazi/rab...done.
(gdb) b printstring
Breakpoint 1 at 0x80484db: file RabinKrap.c, line 14.
(gdb) r
Starting program: /home/nazi/rab 

Breakpoint 1, printstring (inputString=0xbffff2e0 "12345\n\n1234567\004")
    at RabinKrap.c:14
14      int i=0;
(gdb) n
16      while((*(inputString+i))!='\0'){
(gdb) 
17          char c=(*(inputString+i));
(gdb) 
18          printf("%c",c);
(gdb) p inputString
$1 = 0xbffff2e0 "12345\n\n1234567\004"
(gdb) n
19          i++;
(gdb) 
16      while((*(inputString+i))!='\0'){
(gdb) p inputString
$2 = 0xbffff2e0 " \212-"
(gdb) 

1 个答案:

答案 0 :(得分:2)

因为您无法将poitner返回到本地数组,所以它会被释放并且在函数返回后内容会丢失

试试这个,

char *
readfile(FILE *fp)
{
    char *content;
    char chr;
    size_t count;
    // Seek to the end of the file
    fseek(fp, 0L, SEEK_END);
    // Now, the current file position
    // is the size of the file
    count = ftell(fp);
    // Allocate space on the heap so that
    // it's still valid after returning from
    // this function
    content = malloc(count + 1);
    // Always check for errors, if this is
    // NULL there was no enough memory
    if (content == NULL)
        return NULL;
    // Reset the file pointer position
    rewind(fp);
    // Read `count' bytes into the buffer
    if (fread(content, 1, count, fp) != count) {
        // On failure, cleanup
        free(content);
        // Do not call `fclose()' because this
        // function did not call `fopen()'
        return NULL;
    }
    // '\0' Terminate the string (so it becomes A STRING)
    content[count] = '\0';
    // Finish, return it now
    return content;
}