-
fscanf search file HELP!
Yeah im pretty new to programming, but I need help understanding fscanf
my code is supposed to scan a file for "abc" and the print the next word that comes after, then keep going in search of another "abc" and do the same
here is the file to search
Code:
abc Purdue sanctions abc professor for research misconduct, abc
Abc Purdue's undergraduate engineering abc, business ranked among tops in nation abc
abc Aug. 28 - Black Cultural Center abc abc abc announces fall 2008 Cultural Arts Series
Aug. 28 - Activities Bonanza to feature variety of student organizations abc abc abc
then here is my attempt at writing code, its definitely unfinished
Code:
#include<stdio.h>
#define ABC 4
int main()
{
FILE *finput;
finput = fopen("input","r");
search(finput);
fclose(finput);
return(0);
}
void search(finput )
{
int i,abc[ABC]={"a","b","c",0},test[ABC];
for(i=0,)
{
fscanf(finput,"abc",test[ABC]);
if(strcmp(test,abc)==0)
{
fprintf("%d",&abc);
fscanf(finput,32);
}
}
}
-
You can not initialize an integer array with three strings and a 0.
You should re-read about fscanf to see how input is done.
You should re-read about fprintf to see how output is done.
-
Don't forget your function prototypes or types
Code:
void search(FILE *finput)
Also, don't forget to do some kind of error checking when you open the file.
Code:
finput = fopen("input","r");
if (finput == NULL)
errorhandling();
Are you trying to declare everything here as an integer?
Code:
int i,abc[ABC]={"a","b","c",0},test[ABC];
for(i=0,)? Try using fgets in a while loop to input data line by line from the file. Also, strcmp() will compare entire strings. Use strncmp to compare only a specific number of characters.
Code:
FILE *pfile = fopen("somefile.txt", "r");
if (pfile == NULL)
return FALSE;
while (fgets(buffer, BUFFER_LENGTH, pfile) != NULL) {
strncmp(buffer, "abc", 3);
//dostuff
}
Don't forget to append a line feed (\n) to the end of printed data. I don't know the correct terminology for this, but text won't appear on the console until you do.
Code:
fprintf("%d\n",&abc);
Good luck! :)
-
awesome thanks
what i was trying to do in the declare step, was to make abc[ABC] equal "abc" and i put the /o thing on the end, or i was trying to, and i guess thats not how it works...
im still unsure how fscanf works, i re-read it and everything, but its still confusing to me
-
Other things I forgot to mention: the whole point of #define is to have a meaningful name for a constant -- ABC is not a meaningful name for the constant 4.
If you know scanf, then you know fscanf. You may find it easier, as mentioned above, to read in line at a time with fgets and then search inside that string. If you're allowed to use them, I would look at the string handling functions in <string.h> if I were you.
-
thanks a lot, both of you, i got it!! thanks