what do you use to read a line of input (with spaces) until you reach '\n' from a file? fscanf stops at each blank space between words and i don't know how many words are on a line.
thanks.
what do you use to read a line of input (with spaces) until you reach '\n' from a file? fscanf stops at each blank space between words and i don't know how many words are on a line.
thanks.
One way is to use fgets()
data.txt is a file in the same folder as the executable you create when you compile the code.Code:#include <stdio.h> #include <stdlib.h> #include <string.h> #define MAXLINELENGTH 500 int main(void){ FILE *fp; char line [MAXLINELENGTH]; char filename[]="data.txt"; fp=fopen(filename, "r"); if(fp==NULL){ printf("cannot open file called %s\n", filename); abort(); } fgets(line, MAXLINELENGTH, fp); printf("%s",line); fgets(line, MAXLINELENGTH, fp); printf("%s",line); return 0; }
data.txt contains
this line of text happily resides in data.txt
this line does too.
Last edited by petermichaux; 12-02-2003 at 10:05 PM.
ok, i read thw hole line like so... but if the format is:
1;Name One
2;Name Two Two
3;Name Three
...
10;Name Ten
how do i drop the first part up to ";" and keep only the name?
(i know java has a function like so, but C? )
> how do i drop the first part up to ";" and keep only the name?
char *p = strchr( line, ';' );
if ( p != NULL ) printf("%s",p+1);
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.
how do you store that in an array instead of printing it?
basically i'm supposed to have arrnames[10][30] and fill it with those names after i drop the number and ; in front. you can't assign a pointer, how do you get the string to put it in there?
>how do you store that in an array instead of printing it?
Use strncpy instead of printf. Are you that helpless? Copying a string into an array is a basic operation that is described in every book on C, every tutorial on C, and at least ten times a day on this forum.
My best code is written with the delete key.
it appears i am.
I've had strings mentioned only last week, and only how to read them from the input.
i don't have a problem reading more about it, but i'm not even sure what to lok for when i need something like this. Now i know, thanks.
Sorry to bug you.
P.S. got it working, thank you very much.
Last edited by rox; 12-03-2003 at 09:00 AM.
>but i'm not even sure what to lok for when i need something like this
A good reference is immensely helpful. I suggest The C Programming Language to get you started out. That way you have both a good (if not absolutely complete) reference as well as a wonderful tutorial on the majority of the language.
>Sorry to bug you.
For the most part, if you don't do your homework, you will get answers, but possibly not the answers you want, and usually in an irritated tone. Before you post a question, please take the time to consider whether those of us who have been here for years have answered it an uncountable number of times already, that's a big factor in getting sarcastic or rude responses.
My best code is written with the delete key.