New to C : combining lines in text files

This is a discussion on New to C : combining lines in text files within the C Programming forums, part of the General Programming Boards category; Hi, I'm really new to C and I've been doing some exercises to get to grips with it. I am ...

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    57

    Post New to C : combining lines in text files

    Hi,

    I'm really new to C and I've been doing some exercises to get to grips with it. I am learning about file I/O atm, and I've read the through the tutorial on this website.

    e.g.
    separator string is '--' fileOne contains fst line : hello fileTwo contains fst line : world
    my program aims to print "hello--world"

    So yeah any pointers (pardon the pun) or links to tutorials would be great too.

    Code:
    #include <stdio.h>
    int main(int argc, char *argv[])
    {
    	// check num of args
    	if(argc < 3)
    	{
    		fprintf(stderr,"error: not enough arguments\n")	
    	}
    	else
    	{
    		FILE *fileOne =fopen( argv[1], "r");
    		FILE *fileTwo =fopen( argv[2], "r");
    		if ( fileOne == 0 | fileTwo == 0)
    		{
    			/* error*/
     			fprintf(stderr, "Error: Can't open file: %d: %s\n", errno, strerror(errno))
    			
    		}
    		else
    		{
    			int x;
    			while ( ( x fgetc ( fileOne ) ) !=EOF )
    			{
    			//not sure what to do..
    			}
    		}
    	}	
    
    	return;
    }
    Thanks for any help/advice in advance!
    Last edited by play_fetch; 03-06-2011 at 11:33 AM.

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    This is actually pretty easy...
    1) Open your files as you have.
    2) Read a line from file A
    3) Read a line from file B
    4) Use strcat() to add the separator to the line from File A
    5) Use strcat() again to add the line from File B
    6) Display it or write it to a third file.
    7) start again from step 2

    You'll probably want to use fgets() instead of fgetc()

    Just be sure you have lots of space for the lines from file A ... char LineA[512] or so.

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    57
    Quote Originally Posted by CommonTater View Post
    This is actually pretty easy...
    1) Open your files as you have.
    2) Read a line from file A
    3) Read a line from file B
    4) Use strcat() to add the separator to the line from File A
    5) Use strcat() again to add the line from File B
    6) Display it or write it to a third file.
    7) start again from step 2

    You'll probably want to use fgets() instead of fgetc()

    Just be sure you have lots of space for the lines from file A ... char LineA[512] or so.
    Thanks for the fgets tip.

    I'm going to look up the functions you have told me about and how to use them. Seems to be okay... Do you think I need more space for file B as well or just lots for A because I'm just adding to A?

    How can I account for if there are missing lines/empty lines I am wondering... to make sure that it just would print hello-- if world wasn't there. I'm not sure if I need to write something extra so that it does this. It seems you need to think about how to do things in C compared to other languages I know.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by play_fetch View Post
    Thanks for the fgets tip.

    I'm going to look up the functions you have told me about and how to use them. Seems to be okay... Do you think I need more space for file B as well or just lots for A because I'm just adding to A?
    You're going to need an adequate buffer for both files but a needs to be at least twice as big as b because, in effect you are copying b into a plus some extras.

    How can I account for if there are missing lines/empty lines I am wondering... to make sure that it just would print hello-- if world wasn't there. I'm not sure if I need to write something extra so that it does this.
    A text file should not have missing lines. At minimum it will have a newline (\r \n) in a blank line so that should not be an issue. If you skip those lines you will end up with the text misaligned between sides of the final file.

    It seems you need to think about how to do things in C compared to other languages I know.
    Think of C as a totally obedient idiot. You have to tell it every little thing to do, and in the right order... but if you tell it the wrong thing, it will do that too. The language does not baby sit or protect in any way ... if you want something to happen you have to make it happen yourself.
    (This is both it's power and it's weakness)

  5. #5
    Registered User
    Join Date
    Mar 2011
    Posts
    57
    Okay, not sure if I'm still a bit off.. but... I think I have roughly the right idea now.. I really have been doing C for about a week and I'm really confused by it having learnt other languages, which are very different (such as Haskell).

    Code:
    #include <stdio.h>
    int main(int argc, char *argv[])
    {
    	// check num of args
    	if(argc < 3)
    	{
    		fprintf(stderr,"error: not enough arguments\n")	
    	}
    	else
    	{
    		FILE *fileOne =fopen( argv[1], "r");
    		FILE *fileTwo =fopen( argv[2], "r");
    		char sep = argv[0]
    		char LineA[512]
    		char LineB[256]
    		if ( fileOne == 0 | fileTwo == 0)
    		{
    			/* error*/
     			fprintf(stderr, "Error: Can't open file: %d: %s\n", errno, strerror(errno))
    			
    		}
    		else
    		{
    			int x;
    			while( (x fgets(LineA,100, fileOne) && fgets(LineB,100, fileTwo) ) !=EOF)
    			{	
    				strcat(LineA, sep);
    				strcat(LineA, LineB);		
    			}
    		}
    	}	
    
    	return;
    }
    What I THINK this should be doing is while it isn't at the end of file a or b keep going and reading in the next line,
    then add the line sep to file a, then add the next line from b.. possibly need to just give LineB instead of fgets on the 2nd while thing.. and add some {'s.. >.<
    Last edited by play_fetch; 03-05-2011 at 01:22 PM.

  6. #6
    Registered User
    Join Date
    Mar 2011
    Posts
    57
    Quote Originally Posted by CommonTater View Post
    Think of C as a totally obedient idiot. You have to tell it every little thing to do, and in the right order... but if you tell it the wrong thing, it will do that too. The language does not baby sit or protect in any way ... if you want something to happen you have to make it happen yourself.
    (This is both it's power and it's weakness)
    I totally agree with you on that one. The other day I wrote something in C trying to remove trailing spaces - manually using pointers not other functions... However I didn't realise I had to tell it not to read before the beginning of the string I was giving it... >.<

    I'm used to being pampered with Java (one of my first langs) doing everything you need to tell C to do by itself.
    Last edited by play_fetch; 03-05-2011 at 01:20 PM.

  7. #7
    Registered User
    Join Date
    Mar 2011
    Posts
    57
    Currently I have...
    Now trying to figure out how to fix the errors...

    Code:
    #include <stdio.h>
    
    int main(int argc, char *argv[])
    {
    	// check num of args
    	if(argc < 3)
    	{
    		fprintf(stderr,"error: not enough arguments\n");	
    	}
    	else
    	{
    		FILE *fileOne =fopen( argv[1], "r");
    		FILE *fileTwo =fopen( argv[2], "r");
    		char *sep = argv[0];
    		char LineA[512];
    		char LineB[256];
    		if ( fileOne == 0 || fileTwo == 0)
    		{
    			/* error*/
     			fprintf(stderr, "Error: Can't open file");
    			
    		}
    		else
    		{
    			int x;
    			while( (x fgets(LineA,100, fileOne) && fgets(LineB,100, fileTwo) ) !=EOF) //error here
    			{	
    				strcat(LineA, sep); //error here
    				strcat(LineA, LineB);		
    				printf(LineA);
    			}
    			
    		}
    		
    	}	
    
    	return 0;
    }

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    After you take in your first string, just move (overwrite) the newline char, with a space: ' ', char. Now do your strcat of line B, and you're all set.

    So line A was "text\n\0", and now it's "text \0". Strcat can now add line b, and you're done.
    Last edited by Adak; 03-06-2011 at 09:49 AM.

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    while( (x fgets(LineA,100, fileOne) && fgets(LineB,100, fileTwo) ) !=EOF) //error here
    Why is that in there?


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

  10. #10
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by play_fetch View Post
    I'm used to being pampered with Java (one of my first langs) doing everything you need to tell C to do by itself.
    I see the guys are giving you good advice so I won't add to the confusion...

    But... While "baby languages" like Java, Basic and .Net may be easier and faster to develop code in they also place serious limitations upon your ability to optimize and refine your code. All that pampering leads to laziness... Which you will discover the first time you convert a program over from one of them and find out that C code is usually 4 to 6 times faster and 1/5th the size.

    The thing that keeps bringing me back to C, no matter what I try, is that everything is right there, under my control, so I can work with the code to find the best ways of doing things. It comes down to that old saw: "Anything worth doing at all is worth doing right."

  11. #11
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I see the guys are giving you good advice so I won't add to the confusion...
    But we know you want to...

  12. #12
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Adak View Post
    But we know you want to...
    Naaaa... I'll wait till you're 40 or 50 responses along and then pop in with the 5 line solution, just like usual ...

  13. #13
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,162
    Quote Originally Posted by CommonTater View Post
    But... While "baby languages" like Java, Basic and .Net may be easier and faster to develop code in they also place serious limitations upon your ability to optimize and refine your code. All that pampering leads to laziness... Which you will discover the first time you convert a program over from one of them and find out that C code is usually 4 to 6 times faster and 1/5th the size.
    You keep making these claims. Go ahead and come up with a simplish program in C. I'll recreate it in C# and then we'll compare benchmarks and executable size. My prediction: C will be maybe 2 times faster and have a larger executable size. And I won't cheat by using unsafe code in C# (unless you make me).
    If you understand what you're doing, you're not learning anything.

  14. #14
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by itsme86 View Post
    You keep making these claims. Go ahead and come up with a simplish program in C. I'll recreate it in C# and then we'll compare benchmarks and executable size.
    Ok... Give me a day or two and I'll take you up on that over in the "general" forum...
    Should be interesting. Maybe we can get a couple of the others to try it in various other languages, too...

  15. #15
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,162
    Quote Originally Posted by CommonTater View Post
    Ok... Give me a day or two and I'll take you up on that over in the "general" forum...
    Should be interesting. Maybe we can get a couple of the others to try it in various other languages, too...
    Alright. And my apologies to the OP for the hijack.
    If you understand what you're doing, you're not learning anything.

Page 1 of 4 1234 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading text files on the internet
    By Mavix in forum C# Programming
    Replies: 8
    Last Post: 06-20-2007, 05:38 AM
  2. Modifying Text Files
    By quantum_coffee in forum Tech Board
    Replies: 5
    Last Post: 11-18-2006, 01:15 AM
  3. Batch file programming
    By year2038bug in forum Tech Board
    Replies: 10
    Last Post: 09-05-2005, 03:30 PM
  4. Question About Blank Lines in Text Files
    By Zildjian in forum C++ Programming
    Replies: 11
    Last Post: 10-16-2004, 04:31 PM
  5. Text Mode to more than 25 lines
    By karsch1 in forum C Programming
    Replies: 3
    Last Post: 08-31-2001, 01:21 PM

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