-
File I/O lab
I am just stuck on the syantax I know exactly what I need it to do but unsure how to syntactical out put it to a file made a comment on it below //
Code:
#include<stdio.h>
#include <stdlib.h>
int main(int argc, char* argv[])
{
FILE *ptr_mail,out_mail;
char sentence[1000];
char sent[1000];
int i=0,pos=1;
ptr_mail =fopen(argv[1],"r");
if (!ptr_mail)
{
return 1;
}
//gets input from file prints by the sentence until AT_EOS
while (fgets(sentence,1000, ptr_mail)!=NULL)
{
if(sentence[i]=='F'&& sentence[i+1]=='r' &&sentence[i+2]=='o'&&sentence[i+3]=='m' && sentence[i+4]==' ' )
{
//how do I put this all in oneline and too a file ?
printf("%d",pos);
printf(" ");
printf("%s",sentence);
//need to figure out how to put it to output file
int fputs ( const char, FILE *stream );
out_mail= fopen(outputFilename, "w");
pos++;
}
}
fclose(ptr_mail);
return 0;
}
-
You may want to check out the fprintf() function to replace your printf().
Jim
-
That makes sense it would put whatever in fprintf(...) to output but how do I actually declear it right now I have these long lists of errors b/c it doesn't like
int fputs ( const char, FILE *stream );
-
Why are you trying to redefine fputs? Line 27 in the above code is a function definition, not a function call. Plus you are trying to define it differently than how it is defined in stdio.h.
Jim
-
Its something my TA said i should use so I threw it in there other then that no idea lol.
-
Code:
#include<stdio.h>
#include <stdlib.h>
#include <iostream>
int main(int argc, char* argv[])
{
FILE *ptr_mail,*out;
char sentence[1000];
char sent[1000];
int i=0,pos=1;
ptr_mail =fopen(argv[1],"r");
if (!ptr_mail)
{
return 1;
}
//gets input from file prints by the sentence until AT_EOS
while (fgets(sentence,1000, ptr_mail)!=NULL)
{
if(sentence[i]=='F'&& sentence[i+1]=='r' &&sentence[i+2]=='o'&&sentence[i+3]=='m' && sentence[i+4]==' ' )
{
//alright I think I have fprintf working now I cant figure out the syantx to put it to the Isteam or just a file.
out = fopen(output.txt, "w");
if(out != NULL)
{
fprintf(out,"%d",pos);
fprintf(out," ");
fprintf(out,"%s",sentence);
}
//need to figure out how to put it to output file
pos++;
}
}
fclose(ptr_mail);
return 0;
}
-
I suggest you look the documentation for fopen(), pay particular attention to the open modes. What happens when you open a file with the "w" specifier? Why are you continually opening the file in your loop without closing it? Why do you have the fopen() call inside your loop?
Jim