Problems with returning a char* to fprint.

This is a discussion on Problems with returning a char* to fprint. within the C Programming forums, part of the General Programming Boards category; I have started writing a get_line function, designed to copy a line from file1 to file2. It returns a char* ...

  1. #1
    Temporal Apparition qubit67's Avatar
    Join Date
    Jan 2007
    Posts
    85

    Problems with returning a char* to fprint.

    I have started writing a get_line function, designed to copy a line from file1 to file2. It returns a char* pointing to the start of the line back to the main to be used in fprint, but i keep on getting garbled output in file2. I think i may have a problem with assigning my char*? here is my coding so far:
    Code:
    /*This program takes two filenames as command line arguments. It will read the first file, one line at a time, and write each line in reverse to the second file.  The order of the lines in the first file is preserved in the second file, but the content of each line is reversed
    
    For example if the first file contains :
    
    This is the 
    first file. It has
    three lines. 
    
    The second file will contain:
    
    eht si sihT
    sah tI .elif tsrif
    .senil eerht 
    */
    
    /*Libraries*/
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <ctype.h>
    
    /*Function prototypes*/
    
    FILE *safe_open (char*, char*);
    char *get_line(FILE*);
    
    
    int
    main (int argc, char **argv) {
    /*Main*/
    
    	FILE *file1;
    	FILE *file2;
    		
    	file1= safe_open(argv[1],"r");
     	file2= safe_open(argv[2],"w");
    	
    	char *line = get_line(file1);
    	
    	fprintf(file2, "%s", line);
    	
    	
    	fclose(file1);
     	fclose(file2);
    
    	return 0;	
    }
    
    /*Functions*/
    
    char *get_line(FILE *file) {
    /*similar to fgets except that it can read a line of text of unbounded size*/
    	FILE *fp = file;
    	
    	int i = 0 ;
    	char c;
    	char buffer[100];
    	char *line;
    	
    	line = buffer;
    /*read from file ptr until newline or EOF is reached*/
    	while ((c = getc(fp)) != EOF) {
    		buffer[i++] = c;
    	}
    	if (c == '\n')
    		buffer[i++] = c; 
    	
    	buffer[i] = '\0'; 
    
    	return strcpy(line, buffer);	
    }
    
    FILE *safe_open (char *file_name, char *mode)
    {
       FILE *fp;
    
       if ((fp = fopen (file_name, mode)) == NULL)
       {
          fprintf (stdout, "Could not open file: %s\n", file_name);
          exit (EXIT_FAILURE);
       }
       return fp;
    }

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    In function get_line, line is declared as a pointer to char. To store something there, you first have to call malloc() to allocate some space for the data. A simpler way is:
    In main:
    Code:
    	char line[100];
    .
    .
    	get_line(file1, line);
    Code:
    void get_line(FILE *file, char *buffer) {
    >.
    >	char c;
    Declare c as an int. Otherwise the test for EOF may not work. See the FAQ entry for why.

    Remove these lines from get_line:
    > char buffer[100];
    > char *line;

    > return strcpy(line, buffer);
    Last edited by swoopy; 08-08-2007 at 08:06 PM.

  3. #3
    Temporal Apparition qubit67's Avatar
    Join Date
    Jan 2007
    Posts
    85
    Ok so i tried a few different solutions, including your swoopy, both worked with a printf, but my problem is that the function has to return a char* to the start of the line stored in the buffer, so that it can be printed via fprintf to a file. It is only allowed FILE* as an argument.

    So for the function I have to take in the FILE*, create a buffer with nominal size(I'll have to work out a malloc for it later to be dynamic) inside the function, then getc form the FILE* into the buffer including the new line, then return a pointer to the buffer so it can be written to the second empty file.

    Maybe I should make the buffer a global variable for the moment, I think the local variable in the function is probably destroyed upon return.

    BTW here are my experiments so far:
    Code:
    *Libraries*/
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <ctype.h>
    
    /*Function prototypes*/
    
    FILE *safe_open (char*, char*);
    void print_line(FILE*);
    
    
    int
    main (int argc, char **argv) {
    /*Main*/
    
    	FILE *file1;
    	FILE *file2;
    
    	file1= safe_open(argv[1],"r");
     	file2= safe_open(argv[2],"w");
    	
    	print_line(file1);
    	
    	
    	fclose(file1);
     	fclose(file2);
    
    	return 0;	
    }
    
    /*Functions*/
    
    void print_line(FILE *file) {
    
    	FILE *fp = file;
    	
    	int i = 0 ;
    	char c;
    	char buffer[100];
    	
    	
    	while ((c = getc(fp)) != EOF) {
    		buffer[i++] = c;
    	}
    	if (c == '\n') {
    		buffer[i++] = c; 
    	}
    	buffer[i] = '\0'; 
    
    	for (i=0; i<100; i++) {
    		printf("&#37;c", buffer[i]);
    	}
    	putchar('\n');
    }
    
    FILE *safe_open (char *file_name, char *mode)
    {
       FILE *fp;
    
       if ((fp = fopen (file_name, mode)) == NULL)
       {
          fprintf (stdout, "Could not open file: %s\n", file_name);
          exit (EXIT_FAILURE);
       }
       return fp;
    }
    output:
    Code:
     ./simple_test2 file1.txt file2.txt 
    Hello This is a test to see if the get line funstion works.
    ���������ކ
    and
    Code:
    /*Libraries*/
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <ctype.h>
    
    /*Function prototypes*/
    
    FILE *safe_open (char*, char*);
    void get_line(FILE*, char*);
    
    
    int
    main (int argc, char **argv) {
    /*Main*/
    
    	FILE *file1;
    	FILE *file2;
    	char line[200];
    	int i;
    			
    	file1= safe_open(argv[1],"r");
     	file2= safe_open(argv[2],"w");
    	
    	get_line(file1, line);
    	
    	for (i=0; i<200; i++) {
    		printf("%c", line[i]);	
    	}
    	putchar('\n');
    	
    	fclose(file1);
     	fclose(file2);
    
    	return 0;	
    }
    
    /*Functions*/
    
    void get_line(FILE *file, char *buffer) {
    /*similar to fgets except that it can read a line of text of unbounded size*/
    	FILE *fp = file;
    	
    	int c, i = 0 ;
    	
    	
    /*read from file ptr until newline or EOF is reached*/
    	while ((c = getc(fp)) != EOF) {
    		buffer[i++] = c;
    	}
    	if (c == '\n') {
    		buffer[i++] = c; 
    	}
    	buffer[i] = '\0'; 
    	
    }
    
    FILE *safe_open (char *file_name, char *mode)
    {
       FILE *fp;
    
       if ((fp = fopen (file_name, mode)) == NULL)
       {
          fprintf (stdout, "Could not open file: %s\n", file_name);
          exit (EXIT_FAILURE);
       }
       return fp;
    }
    output:
    Code:
     ./simple_test file1.txt file2.txt 
    Hello This is a test to see if the get line funstion works.
    T_߿�_߿h��.N=�{߿�G�8_߿���
    file2 is not used, io is through printf

  4. #4
    Temporal Apparition qubit67's Avatar
    Join Date
    Jan 2007
    Posts
    85
    I got it to work with the requirements of my last post. Though I had to use a globally defined array. Is there a way to do it without using one?
    so far :
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <ctype.h>
    
    FILE *safe_open (char*, char*);
    char *get_line(FILE*);
    
    char buffer[50];
    
    int
    main (int argc, char **argv) {
    
    	FILE *file1;
    	FILE *file2;
    
    	file1= safe_open(argv[1],"r");
     	file2= safe_open(argv[2],"w");
    	
    	fprintf(file2, "&#37;s", get_line(file1));
    	
    	fclose(file1);
     	fclose(file2);
    
    	return 0;	
    }
    
    char *get_line(FILE *file) {
    
    	FILE *fp = file;
    	
    	int c, i = 0;
    	char *m;
    	
    	
    	while ((c = getc(fp)) != EOF) {
    		buffer[i++] = c;
    	}
    	if (c == '\n') {
    		buffer[i++] = c; 
    	}
    	buffer[i] = '\0'; 
    
    	m = malloc(i+1);
    	strcpy(m, buffer);
    	return m;
    }
    
    FILE *safe_open (char *file_name, char *mode)
    {
       FILE *fp;
    
       if ((fp = fopen (file_name, mode)) == NULL)
       {
          fprintf (stdout, "Could not open file: %s\n", file_name);
          exit (EXIT_FAILURE);
       }
       return fp;
    }
    out :
    Code:
    : cat file2.txt 
    Hello This is a test to see if the get line funstion works.

  5. #5
    Temporal Apparition qubit67's Avatar
    Join Date
    Jan 2007
    Posts
    85
    ok, i think i worked it out, I have to resize the buffer somehow, another function, this within getline should do it

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    In your second example:
    Code:
    >	get_line(file1, line);
    >	
    >	for (i=0; i<200; i++) {
    >		printf("&#37;c", line[i]);	
    >	}
    In case you weren't aware, you could write it to the file as follows:
    Code:
    	fprintf(file2, "%s", line);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. No clue how to make a code to solve problems!
    By ctnzn in forum C Programming
    Replies: 8
    Last Post: 10-16-2008, 03:59 AM
  2. Problems passing a file pointer to functions
    By smitchell in forum C Programming
    Replies: 4
    Last Post: 09-30-2008, 03:29 PM
  3. Problems returning char *
    By Necromancer in forum C Programming
    Replies: 10
    Last Post: 01-30-2008, 11:28 AM
  4. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 11:03 AM
  5. Returning references (scope problems?)
    By DarkDragon in forum C++ Programming
    Replies: 3
    Last Post: 12-23-2001, 07:48 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21