D
dough
I want to read in lines from a file and then seperate the words so i
can do a process on each of the words. Say the text file "readme.txt"
contains the following:
In the face of criticism from the left and right, President Bush
insisted Tuesday that Harriet Miers is the nation's best-qualified
candidate for the Supreme Court and assured skeptical conservatives
that his lawyer...
I could get an input to a char *s such that s = "In" and then i do
something with s, then s = "the" and then i do something with that,
etc. With no idea the length of any string or line or whitespace.
Heres what I have so far.
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void process(char *s) /* whats here is not really important *
{
printf("%s", s);
}
int main() {
char buffer[80];
FILE *f = fopen("readme.txt", "r");
char *s;
while( fgets(buffer, sizeof(buffer), f) != NULL ) /* reads a line */
{
while( sscanf(buffer, "%s", s) ) /* scans for words in line */
{
process(s); /* do stuff to the words */
}
}
fclose(f);
return 0;
}
Also, is there anyway to adjust the size of the buffer or reallocate
the memory so it doesn't overflow and get a seg error.
can do a process on each of the words. Say the text file "readme.txt"
contains the following:
In the face of criticism from the left and right, President Bush
insisted Tuesday that Harriet Miers is the nation's best-qualified
candidate for the Supreme Court and assured skeptical conservatives
that his lawyer...
I could get an input to a char *s such that s = "In" and then i do
something with s, then s = "the" and then i do something with that,
etc. With no idea the length of any string or line or whitespace.
Heres what I have so far.
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void process(char *s) /* whats here is not really important *
{
printf("%s", s);
}
int main() {
char buffer[80];
FILE *f = fopen("readme.txt", "r");
char *s;
while( fgets(buffer, sizeof(buffer), f) != NULL ) /* reads a line */
{
while( sscanf(buffer, "%s", s) ) /* scans for words in line */
{
process(s); /* do stuff to the words */
}
}
fclose(f);
return 0;
}
Also, is there anyway to adjust the size of the buffer or reallocate
the memory so it doesn't overflow and get a seg error.