FILE I/O is driving me nuts! Need Help. (C Programming)

wizzz

Senior member
Jul 20, 2000
267
0
0
Hello all.
I'm trying to write a very simple File I/O using C.

This is what I have up til now.

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

#define SIZE 100

int main( char *argv[] ){
^^^^^^^^^^^^^^^^^^^^^^^^
What is this for???(the parameter char *argv[]) I didn't know what to put so I just put this.(char *argv[])
What should be going in here in general?


FILE *input, *output;
int i;
char buffer[SIZE];

for(i=0; i < SIZE; i++){
buffer = '\0';
}

input=fopen(&quot;input.txt&quot;,&quot;r&quot;);

if(!input)
{
fprintf(stderr,&quot;COULD NOT OPEN INPUT FILE\n&quot;);
fflush(stderr);
return EXIT_FAILURE;
}

output=fopen(&quot;output.txt&quot;,&quot;w&quot;);

if(!output)
{
fprintf(stderr,&quot;COULD NOT OPEN OUTPUT FILE\n&quot;);
fflush(stderr);
return EXIT_FAILURE;
}

while(fscanf(input,&quot;%[^\n]&quot;, buffer)!=EOF){


fprintf(output, &quot;%s\n&quot;, buffer);
}

fclose(input);
fclose(output);

return EXIT_SUCCESS;

}


When I compile this, C doesn't complain.
I compiled it as such: gcc -o test test1.c

When I ran the executable file, test, I got stuck in an infinite loop!
output.txt was created but with infinite lines of the first line read in from the input file.


input.txt has 4 lines. Each line containing around 5 -10 characters joined together. So they are strings (or array of characters).

So why do I get stuck in an infinite loop?
I spent a lot of time trying everything. Nothing worked for me.
I need help. I'm almost going to punch my computer screen...arghghhghg

Any help is appreciated.