2017-03-12 1 views
0

は、ここで私はそれをプリントを処理する機能を印刷するFRM txtファイルを読み込み、文字列を渡したい、私のコードであること:なぜポインタ値が変わるのですか?

#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 

出力:

[email protected]:~$ gcc -o rab RabinKrap.c -g 
[email protected]:~$ ./rab 
1�[email protected]:~$ 

whileループでの割り当て後に、何か他のものに再初期化されました。

[email protected]:~$ 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

をお試しください'fopen'からの戻り値をチェックしていませんか? –

+0

@EdHeal私はあなたの質問を得ていません –

+0

'fopen'はNULLを返すことができます。あなたはこれをテストする必要があります。マニュアルページを参照してください。 –

答えて

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; 
} 
関連する問題