Thread: copying file into string - not working?

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    29

    copying file into string - not working?

    hi,

    im just simply copying the whole contents of a file from the net into a string in my program and printing it out...this doesnt work,,any suggestions on how to fix or do it another way?

    Code:
    FILE *p;
    	char *file = NULL;
    	long fileSize = 0;	
    	
    	if((p = popen("/usr/other/sdm/Bin/wget -q -O - http://homepages.ihug.com.au/~pooni/xml/test.xml","r")) == NULL) {
    		
    		fprintf(stderr, "cannot create pipe \n");
    		exit(1);		
    	}
      	
    	fseek(p, 0, SEEK_END);
    	fileSize = ftell(p);
    	rewind(p);
    	
    	if (fileSize < 1){
    		fprintf(stderr, "File has no data\n");
    		exit(1);
    	}
    	
    	file = (char *) calloc((fileSize+1), sizeof(char));
    	
    	if (!file){
    		fprintf(stderr, "Unable to allocate %d bytes of memory\n", fileSize);
    		exit(1);
    	}
    
    	fread(file, fileSize, 1, p);
    	file[fileSize] = '\0';
    	
    	printf("File is %ld bytes. File Data:\n%s\n", fileSize, file);
    
    	free(file);
    	fclose(p);

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    I somehow doubt that you can seek/rewind a pipe
    Try checking the status returns from those functions to see if they return an error

    > or do it another way?
    Like a linked list of lines for example.
    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.

  3. #3
    Registered User
    Join Date
    Apr 2004
    Posts
    29
    is there any other way i can copy the contents of a file into a string..?

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Same way as you handle a normal text file
    Code:
    char buff[BUFSIZ];
    while ( fgets( buff, BUFSIZ, p ) != NULL ) {
      char *line = malloc ( strlen(buff) + 1 );
      if ( line ) {
        strcpy( line, buff );
        // append line to whatever expanding data structure you have
        // I suggested a linked list
      }
    }
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. disposing error
    By dropper166 in forum C# Programming
    Replies: 2
    Last Post: 03-30-2009, 11:53 PM
  2. Headers that use each other
    By nickname_changed in forum C++ Programming
    Replies: 7
    Last Post: 10-03-2003, 04:25 AM
  3. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 11:06 PM
  4. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM