Note that there are some explanatory texts on larger screens.

plurals
  1. POstring from file C
    primarykey
    data
    text
    <p>I'm reading a string from a file and then, trying to add zeros to the string till it reach 100, and then I'm printing the string, but I don't know what's wrong, I've tried more than one approach , but they all seems not to work. </p> <pre><code>int main(int argc, char *argv[]) { if (argc != 2 ){ fprintf(stderr, "usage: server filename \n"); exit(1); } FILE *file = fopen(argv[1], "r"); if (file==0) { printf("file couldn't be opened\n"); exit(1); } int i; char str1[100]; //char str2[100]; //memset(str2,0,sizeof(str2)); //for(i = 0; i &lt; 100; i++) // fprintf(stdout, "str2[%u]: %u\n",i,str2[i]); while (fscanf(file, "%s", str1) != EOF) { for(i=13 ; i &lt; 100; i++) str1[i]=0; } for(i = 0; i &lt; 100; i++) fprintf(stdout, "str1[%u]: %u\n",i,str1[i]); return 0; } </code></pre> <p>but I print it I got </p> <pre><code>str1[0]: 119 str1[1]: 111 str1[2]: 114 str1[3]: 108 str1[4]: 100 str1[5]: 0 str1[6]: 0 str1[7]: 0 up till 99 </code></pre> <p>I don't understand this although there is a string in the file "hello" and then I've tried this</p> <pre><code>int main(int argc, char *argv[]) { if (argc != 2 ){ fprintf(stderr, "usage: server filename \n"); exit(1); } FILE *file = fopen(argv[1], "r"); if (file==0) { printf("file couldn't be opened\n"); exit(1); } int i; char str1[12]; char str2[100]; memset(str2,0,sizeof(str2)); //for(i = 0; i &lt; 100; i++) // fprintf(stdout, "str2[%u]: %u\n",i,str2[i]); while (fscanf(file, "%s", str1) != EOF) { strcpy(str2,str1); } for(i = 0; i &lt; 100; i++) fprintf(stdout, "str2[%u]: %u\n",i,str2[i]); return 0; } </code></pre> <p>and I gave me the exact same result as the first one, so I don't understand what is happening here, and why I'm getting these results. I'll be grateful if you can explain what I'm doing wrong here. Thanks in advance.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload