Thread: Reading data from file

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    2

    Reading data from file

    Hello,

    I have a rather simple program, which reads a string from file and stores it in a char array. While reading the file, each line is saved in the array properly, but when I exit the loop and print the array again, it prints only the last entry. There is probably something silly that I have not taken into account, but I am not able to figure it out.

    Here is the code:

    Code:
    #include <stdio.h>
    
    int main(int argc, char ** argv)
    {
      char **input_list;
      int num_list=0;
      char temp_list[1024] ="";
      char line[4096];
      FILE *wlist = NULL;
      if (argc<=1)
        {
          fprintf(stderr, "Usage: {-list <File listing the strings> \n", argv[0]);
          exit(-1);
        }
    
      while(argc>1)
       if (strcmp(argv[1], "-list")==0) 
          {
    	fprintf(stdout, "foo\n");
    	input_list = (char **)calloc(4096, sizeof(char *));
    	
    	if ((wlist = fopen(argv[2], "r"))!=NULL)
    	  {
    	   
    	    num_list = 0;
    	    while(fgets(line, sizeof(line), wlist)!=NULL)
    	      {
    		sscanf(line, "&#37;s", &(temp_list));
    		input_list[num_list]= (char *)calloc(4096, sizeof(char));
    		input_list[num_list]= (temp_list);
    		fprintf(stdout, "Read string %s\n",input_list[num_list]);
    		num_list++;
    	      }
    	    input_list[num_list] = NULL;
    	   
    	    fclose(wlist);
    	    fprintf(stdout, "Read %d strings\n", num_list);
    	   
    	  }
    	argc--;argv++;
    	argc--;argv++;
          }
       
     int i;
    
     for(i=0;i<num_list;i++)
        {
          fprintf(stdout, "String %s\n", input_list[i]);
        }
    
     free(input_list);
    }
    The input list file looks like this:
    "a"
    "b"
    "c"
    "d"

    And the output is:
    foo
    Read waveform "a"
    Read waveform "b"
    Read waveform "c"
    Read waveform "d"
    Read 4 waveforms
    Waveform "d"
    Waveform "d"
    Waveform "d"
    Waveform "d"


    When I reprint the input_waveforms_list array, it prints the last line that it read for four times. Kindly let me know what is wrong with the code or a better way to write it.
    Thanks.
    Last edited by mvm2008; 07-23-2008 at 10:55 AM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I don't see a strcpy anywhere.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > input_waveforms_list[num_waveforms]= (temp_waveforms_list);
    This points to your array (which you only have one of, hence everything is the same in the end).
    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.

  4. #4
    Registered User
    Join Date
    Jul 2008
    Posts
    2
    Thanks for your suggestions. The problem is fixed.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  2. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  3. reading data from a file
    By brianptodd in forum C++ Programming
    Replies: 1
    Last Post: 11-07-2003, 06:50 PM
  4. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM
  5. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 03:58 PM