How would I begin to write a function that reads a data file (text) in and split it up into separate words?
How would I begin to write a function that reads a data file (text) in and split it up into separate words?
with reading a FAQ? http://faq.cprogramming.com/cgi-bin/...&id=1043284392
All problems in computer science can be solved by another level of indirection,
except for the problem of too many layers of indirection.
– David J. Wheeler
Ok I don't understand any of that. Thanks though.
If you post what you've got so far, and explain where you're stuck, we can help you out.
Jason Deckard
Haven't got anywhere so far. I've just been told I need to create a function that reads in data from a text file.
Let see if this help you
Will that be able you to start it up. And note u havn't done any error checking. I will leave it to you.Code:Delcare a file pointer <-- FILE * fp; Open a file <-- Use fopen Ex: fp = fopen("test.txt","r"); While ( fgets(buf, sizeof buf,fp) != NULL) /* this will read the file into the buffer line by line. */
this would help you
Files I\O
ssharish2005
The following works to read in the text file but also reads in punctuation, is there any way to exclude them?
Code:#include<stdio.h> #include<stdlib.h> #include<string.h> int main(void) { FILE *fp = fopen("file.txt", "r"); char result[128]; while (!feof(fp)) { if (fscanf(fp, "%128s", result)<1) { break; } printf("Found word bit: %s \n", result); } fclose(fp); }
you can use "%128[^, ]" format to read till the first char in the [] after ^ is encountered.
this char is not read, so you should in some way skip this chars till the new word start is encountered
easier to achive - use fgets to read the whole line and then sscanf to parse words
All problems in computer science can be solved by another level of indirection,
except for the problem of too many layers of indirection.
– David J. Wheeler
strspn() and strcspn() may prove useful.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.