Thread: how to use fgets to read strings in a txt file?

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    5

    how to use fgets to read strings in a txt file?

    crying crying gonna die already

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I don't see any tear stains on your post, buddy!

    What have you tried with fgets()? Post the code and tell us what your status is with learning fgets().

    Normally, I'm pretty good with getting an understanding of where people are from their post, but I've been ROFL so much laffing at the drama queens on the programming forums, lately - you know how funny they can be!

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    5
    given for example
    FILE *flevel;
    map *current;
    current=first;
    fgets(flevel,"%s", current->level.levelinfo);
    current->level.levelinfo is my string
    so what exactly is going to happen?

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Some errors - not much else. Wrong format for fgets(). The file pointer is the third parameter for fgets(), not the first. First is the char array name that will be receiving the row of data. Then the size

    When you want to post code:

    1) highlight it in the advanced editor of the forum, and click on the # icon in the editors menu bar.

    2) indent your code. Look at posts from myself, and other regulars - easy to read, and study.

    Poor code that looks like the dog got sick, tends to get skipped.

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Wow... all emotional.... Try this...

    Assuming current->level.levelinfo is a char*...
    Code:
    int GetString(FILE *file)
      {  char *str, *ptr;
         char buffer[128];
        // read disk 
        fgets(buffer,128,file);
        // get rid of newline
        ptr = strchr(buffer, '\n')
        if (ptr)
         *ptr = 0; 
         // allocate string space
         str = malloc((strlen(buffer) +1) * sizeof(char));
         // hand over a right-sized string
         return strcpy(str,buffer);
      }
    
    // in main
    current->level.levelinfo = GetString(fileptr);

  6. #6
    Registered User
    Join Date
    Oct 2011
    Posts
    5
    wats strchr is it like strtok?

  7. #7
    Registered User
    Join Date
    Sep 2011
    Posts
    111
    Quote Originally Posted by needhelp1 View Post
    crying crying gonna die already
    Code:
    #include <stdio.h>
     
    int main()
    {
      // open the text file "fred.txt" for writing
      FILE *out = fopen("fred.txt", "wt");
     
      // write some text to the file
      fprintf(out, "Hello Fred!\n");
     
      // close the stream, so all changes to the file are saved
      fclose(out);
     
      // open the file "fred.txt" for reading
      FILE *in = fopen("fred.txt", "rt");
     
      // read the first line from the file
      char buffer[100];
      fgets(buffer, 20, in);
     
      // display what we've just read
      printf("first line of \"fred.txt\": %s\n", buffer);
     
      // close the stream
      fclose(in);
     
      return 0;
    }

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by needhelp1 View Post
    wats strchr is it like strtok?
    Look it up in your compiler's library documentation ... if you don't have it, get it.

    There's no excuse to have to ask about the syntax or behaviour of functions, this is information every programmer should have.

    What compiler are you using?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. read a number with scanf() and read char[] with fgets()
    By nutzu2010 in forum C Programming
    Replies: 5
    Last Post: 03-11-2011, 05:05 AM
  2. read mix file: fscanf fgets
    By cfdprogrammer in forum C Programming
    Replies: 1
    Last Post: 03-20-2009, 11:38 AM
  3. Replies: 19
    Last Post: 06-16-2008, 05:00 AM
  4. read certain strings from file
    By shatred in forum C++ Programming
    Replies: 2
    Last Post: 10-22-2006, 02:48 PM
  5. Reset fgets to read from the beginning of a file?
    By bivhitscar in forum C Programming
    Replies: 2
    Last Post: 05-20-2006, 11:42 PM