Fgets() & Fputs() to combine text files?

This is a discussion on Fgets() & Fputs() to combine text files? within the C Programming forums, part of the General Programming Boards category; I am trying to get input from two txt files (line by line) and output the two files side by ...

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    42

    Fgets() & Fputs() to combine text files?

    I am trying to get input from two txt files (line by line) and output the two files side by side in colums 38 characters wide. Output should go to a single txt file.

    I have been able to get the input by line and have it output it as follows:
    line 1 txt file 1
    line 1 txt file 2
    line 2 txt file 1
    line 2 txt file 2
    ect.

    What Im trying to get is:
    line 1 (truncated to 38 char) "space space" line 1 (truncated 38 char)
    line 2 (truncated to 38 char) "space space" line 2 (truncated 38 char)

    I am stuck and can't seem to get any further. Here is my code:
    Code:
    #include <stdio.h>
    #include <string.h>
    #define MAX 1000
    
    int main(int argc, char **argv)
    {	
    	char *Path1 = "C:\\Documents and Settings\\Brad\\My Documents\\Visual Studio 2010\\Projects\\Assignment 10\\GettysburgAddress.txt";
    	char *Path2 = "C:\\Documents and Settings\\Brad\\My Documents\\Visual Studio 2010\\Projects\\Assignment 10\\Anthem.txt";
    	char *Path3 = "C:\\Documents and Settings\\Brad\\My Documents\\Visual Studio 2010\\Projects\\Assignment 10\\output.txt";
    		
    	FILE *Getty, *National, *output;
    	
    	char line[MAX];
    	char line2[MAX];
    	char outputline[1000];
    	Getty = fopen(Path1, "r");
    	if (!Getty)
    		return 1;
    
    	National = fopen(Path2, "r");
    	if (!National)
    		return 1;
    
    	output = fopen(Path3, "w");
    	if (!output)
    		return 1;
    
    	while (fgets(line,(MAX),Getty))	
    	{	
    		fputs(line, output);
    		fgets(line2,(MAX),National);
    		//strcat(line, line2);
    		fputs(line2, output);
    		
    	}
    
    	fclose (Getty);
    	fclose (National);
    	fclose (output);
    	
    	getchar();
    	return 0;
    }

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    In your while loop, while doing the copying. If you wish to combine lines you are going to have to do a little more work...

    First you're going to have to load the first line.
    Second, strip off carriage returns from the first line while it sits in your buffer.
    Third, get the second line also in memory before writing the first.
    Fourth, you need to join them while still in memory.
    Finally write them to the output file as a single string.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    Try
    Code:
    fprintf( output, "%30.30s  %30.30s\n", line1, line2 );
    If the lines are short, then you'll need to remove the \n that fgets() reads.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Sep 2010
    Posts
    42
    Thanks for the direction Salem!!! I've got my code working and wrote a function to remove the newline.

  5. #5
    Registered User
    Join Date
    Sep 2010
    Posts
    42
    I guess I spoke too soon! Depending on how I feed it the files it will stop when the shortest .txt file is finished. Only got 1/2 credit since it only works 1/2 the time! Now its just for my info. Any ideas?? Thanks. Here is my code:

    Code:
    #include <stdio.h>
    #include <string.h>
    #define MAX 999
    
    void removenewline( char *string );
    
    int main(int argc, char **argv)
    {	
    	char *Path1 = "C:\\Documents and Settings\\Brad\\My Documents\\Visual Studio 2010\\Projects\\Assignment 10\\GettysburgAddress.txt";
    	char *Path2 = "C:\\Documents and Settings\\Brad\\My Documents\\Visual Studio 2010\\Projects\\Assignment 10\\Anthem.txt";
    	char *Path3 = "C:\\Documents and Settings\\Brad\\My Documents\\Visual Studio 2010\\Projects\\Assignment 10\\output.txt";
    		
    	FILE *Getty, *National, *output;
    	
    	char line1[MAX];
    	char line2[MAX];
    	
    	Getty = fopen(Path1, "r");
    	if (!Getty)
    		return 1;
    
    	National = fopen(Path2, "r");
    	if (!National)
    		return 1;
    
    	output = fopen(Path3, "w");
    	if (!output)
    		return 1;
    
    	while (fgets(line1,(MAX),Getty))	
    	{	
    		removenewline(line1);
    		fgets(line2,(MAX),National);
    		removenewline(line2);
    		fprintf( output, "%-38.38s  %38.38s\n", line1, line2 );
    	}
    
    	fclose (Getty);
    	fclose (National);
    	fclose (output);
    	
    	return 0;
    }
    
    void removenewline( char *string )
    {
        char *ptrtolast;
    	
        ptrtolast = string + strlen(string) - 1;
    	if (*ptrtolast == '\n') 
    		*ptrtolast = 0;
    }

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    Something like this
    Code:
    	while (fgets(line1,(MAX),Getty))	
    	{	
    		removenewline(line1);
    		if ( fgets(line2,(MAX),National) ) {
    			removenewline(line2);
    			fprintf( output, "%-38.38s  %38.38s\n", line1, line2 );
    		} else {
    			// What to print here (remainder if National is shorter)?
    		}
    	}
    
    	while ( fgets(line2,(MAX),National) ) {
    		// what to print here  (remainder lines if Getty was shorter?
    	}
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 02-11-2008, 12:36 AM
  2. How to use FTP?
    By maxorator in forum C++ Programming
    Replies: 8
    Last Post: 11-04-2005, 02:17 PM
  3. Structure problem
    By mattz in forum C Programming
    Replies: 10
    Last Post: 11-30-2001, 12:19 PM
  4. displaying text files, wierd thing :(
    By Gades in forum C Programming
    Replies: 2
    Last Post: 11-20-2001, 04:18 PM
  5. Outputting String arrays in windows
    By Xterria in forum Game Programming
    Replies: 11
    Last Post: 11-13-2001, 06:35 PM

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