Thread: help with fgets and assigning line to variable

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    1

    help with fgets and assigning line to variable

    hello,

    i am trying to paste into a variable the first line i get from a file with fgets. this is done in a loop so if the condition is true it continues and loads the next line from the file into the variable.
    Code:
    char line[80];
    char *mynames;
    long counter = -1L;
    FILE *names;
     
    if ((names = fopen (filename, "r")) == NULL)
    {
    	perror ("fopen");
    	exit (1);
    }
    else
    {
    	for counter = 0L;
    		fgets(line, sizeof(line), names) != NULL;
    		//line = myname;
    		counter += strlen(line));
    I cant understand how i can place the respective line in the variable mynames. could someone explain me please what i am doing wrong. thanks for your help as i am pretty new with c and i could easilly brake my keyboard right now
    Last edited by sean; 04-10-2005 at 07:52 PM. Reason: Code tags

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    When posting code, please use code tags. They format your code to make it easier to read. Here's how:

    http://cboard.cprogramming.com/showthread.php?t=25765

    I've edited your post and put them in for you.

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Do you really want all lines in memory at the same time, or can you make do with having only one line at a time?

    Working with one line is easier and quicker, and is the way I'd recommend for whereever possible.
    Loading all lines is tricky and requires you to either pre-allocate space of a fixed size, thus restricting how many lines can be read, OR, use dynamic memory allocation to allow you to hold an (almost) infinite number of lines. The latter requires more advanced coding practices.

    Decide what you want and let us know.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  4. #4
    Registered User
    Join Date
    Apr 2005
    Posts
    5
    hey there... i'm pretty new to c too, but i've had to do a similar thing when i was sending configuration strings to a gps... but anyway, check this out... works for me.

    Code:
    #include <stdio.h>
    #include <string.h>
    
    void main(void)
    {
    
     FILE *cfgfile;
     int maxline = 256;
     int i=0, j=0;
     char cfgstring[16][256];
    
    
     cfgfile = fopen("strings.txt", "r");
    
     if (cfgfile==NULL) printf("can't open file\n");
    
     while (fgets(cfgstring[i], maxline, cfgfile)) {      // while not EOF
    
        cfgstring[i][strlen(cfgstring[i])-1] = '\0';      // remove CR from line
        i++;
       }
      
      for(j=0; j<i; j++) printf(" cfgstring[%d] = [%s]\n", j, cfgstring[j]);
    
    
     fclose(cfgfile);
    }
    
    
    /*opens this file.... strings.txt
    
    Hello, i am a string
    and i am another
    the end
    
    */
    
    /* results on screen
    
     cfgstring[0] = [Hello, i am a string]
     cfgstring[1] = [and i am another]
     cfgstring[2] = [the end]
    */


    this reads each line of the file strings.txt into the two dimensional array cfgstring [16] by [256]... so up to 16 lines stored, and each line can be no more than 256 bytes... although you can make it as big/small as you need

    it does however requires that is at least a newline after the final line to be read or it'll remove - in my example - the d in end....

    Anyway, thats how i did it.. .hope it helps ya.
    Laters

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Intelicopter
    hey there... i'm pretty new to c too, but i've had to do a similar thing when i was sending configuration strings to a gps... but anyway, check this out... works for me.
    There are a few problems with your code. I'll point them out for you:
    Code:
    void main(void)
    Main is not a void function, ever. The FAQ will give you an explanation of the reason this is wrong. Main always returns an integer.

    Code:
     cfgfile = fopen("strings.txt", "r");
    
     if (cfgfile==NULL) printf("can't open file\n");
    If the file fails to open, you display a message saying it can't open it. Then, you continue on anyway. You should be exiting the program right here, or resolving it some other way. Instead, you just print a message and keep on going, trying to use the file anyway...

    Code:
    cfgstring[i][strlen(cfgstring[i])-1] = '\0';      // remove CR from line
    It's possible here that your line you just read doesn't contain a newline. As such, you'll just end up writing over top of some piece of data. A better way would be perhaps something like this:
    Code:
    char *ptr;
    
    ptr = strchr( cfgstring[i], '\n' );
    if( ptr != NULL )
    {
        *ptr = '\0';
    }

    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Apr 2005
    Posts
    5
    indeed sir, you are right, but i had the right general idea!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 2D Array's, assigning chars.
    By gman89 in forum C Programming
    Replies: 9
    Last Post: 04-26-2008, 11:03 PM
  2. Paging Program Simulator:
    By wise_ron in forum C Programming
    Replies: 7
    Last Post: 10-31-2006, 02:11 PM