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("input.txt","r"
if(!input)
{
fprintf(stderr,"COULD NOT OPEN INPUT FILE\n"
fflush(stderr);
return EXIT_FAILURE;
}
output=fopen("output.txt","w"
if(!output)
{
fprintf(stderr,"COULD NOT OPEN OUTPUT FILE\n"
fflush(stderr);
return EXIT_FAILURE;
}
while(fscanf(input,"%[^\n]", buffer)!=EOF){
fprintf(output, "%s\n", 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.
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("input.txt","r"
if(!input)
{
fprintf(stderr,"COULD NOT OPEN INPUT FILE\n"
fflush(stderr);
return EXIT_FAILURE;
}
output=fopen("output.txt","w"
if(!output)
{
fprintf(stderr,"COULD NOT OPEN OUTPUT FILE\n"
fflush(stderr);
return EXIT_FAILURE;
}
while(fscanf(input,"%[^\n]", buffer)!=EOF){
fprintf(output, "%s\n", 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.