Thread: read a line

  1. #1
    Registered User
    Join Date
    Nov 2003
    Posts
    34

    read a line

    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.

  2. #2
    Registered User
    Join Date
    Nov 2003
    Posts
    46
    One way is to use fgets()

    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 is a file in the same folder as the executable you create when you compile the code.

    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.

  3. #3
    Registered User
    Join Date
    Nov 2003
    Posts
    34
    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? )

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,664
    > 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.

  5. #5
    Registered User
    Join Date
    Nov 2003
    Posts
    34
    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?

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >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.

  7. #7
    Registered User
    Join Date
    Nov 2003
    Posts
    34
    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.

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. "sorting news" assignment
    By prljavibluzer in forum C Programming
    Replies: 7
    Last Post: 02-06-2008, 06:45 AM
  2. OPen a file and read it from the last line
    By c_geek in forum C Programming
    Replies: 14
    Last Post: 01-26-2008, 06:20 AM
  3. easy Q -> read a whole line from file
    By paperbox005 in forum C++ Programming
    Replies: 3
    Last Post: 09-27-2004, 03:58 PM
  4. Read each line of text file.
    By Mithoric in forum C++ Programming
    Replies: 1
    Last Post: 06-25-2003, 11:53 AM
  5. Greenhand want help!
    By leereg in forum C Programming
    Replies: 6
    Last Post: 01-29-2002, 06:04 AM