Thread: How do I make this work with lines of any length

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

    How do I make this work with lines of any length

    Below is the code I have.. I need to make it so it copes with file lines of any length but currently it works with only a fixed number

    Code:
    #include <stdio.h>
    #include <string.h>
    
    
    int main(int argc, char *argv[])
    {
    	// check num of args
    	if(argc != 4)
    	{
    		fprintf(stderr,"Usage is %s <number>\n", argv[0]);
    	}
    	else
    	{
    		FILE *fileOne =fopen( argv[2], "r");
    		FILE *fileTwo =fopen( argv[3], "r");
    		char *sep = argv[1];
    		char LineA[512];
    		char LineB[256];
    		if ( fileOne == 0 || fileTwo == 0)
    		{
    			/* error*/
     			fprintf(stderr, "Error: Can't open file");
    			
    		}
    		else	
    		{			
    			
    			while(*LineA || *LineB)
    			{
    				char *p;
    
    				if(fgets(LineA, sizeof(LineA), fileOne))
      				{
        					if((p = strchr(LineA, '\n')))
    						*p = '\0';
      					
    				}
      				else
        					*LineA = '\0';
    
      				if(fgets(LineB, sizeof(LineB), fileTwo))
      				{
        					if((p = strchr(LineB, '\n')))
          						*p = '\0';
      				}
      				else
        					*LineB = '\0';
    
      				if(*LineA || *LineB)
    				{
        		
    					printf("%s%s%s\n", LineA, sep, LineB);
    				}			
    			
    			}
    		}
    		
    			
    	}
    	return 0;
    }
    thanks in advance for any help advice on this... really need it!

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I answered this in your other thread.

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    57
    Quote Originally Posted by Adak View Post
    I answered this in your other thread.
    I know but I'm really unsure how to do it. I've tried but I can't get it to work.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    So rather than actually try the suggestion made, you decided you would make a new thread in hopes that someone would do everything for you?

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

  5. #5
    Registered User
    Join Date
    Mar 2011
    Posts
    57
    Quote Originally Posted by quzah View Post
    So rather than actually try the suggestion made, you decided you would make a new thread in hopes that someone would do everything for you?

    Quzah.
    No I just I didn't see that response before I created the thread so I didn't think the thread would get resurrected which is why I created a new one just in case. You can check the times of the responses in case you don't believe me, that response was posted 1 minute after I created this thread.

    Also
    I had an idea before that I tried to implement which was to read each line character by character until I reached the new line or EOF.. however I couldn't get that code to work so thought maybe I couldn't do it that way...

    I do have my own ideas about how to implement things I just have trouble writing it.

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Whenever you can, work with strings as strings, not as individual char's. Can't always do it, but 90% of the time (imo), you can and should.

  7. #7
    Registered User
    Join Date
    Mar 2011
    Posts
    57
    OKAY I'm now doing something like
    Code:
    	int i = fgetc(fileOne);
    		        int j = fgetc(fileTwo);
    			while(i != '\n' && i != EOF)
    			{
    				puthcar(i);
    				i = fgetc(fileOne);	
    			}
    			printf(%s, sep)
    			while( j != '\n' && != EOF)
    			{
    				putchar(j);
    				j = fgetc(fileTwo);
    			}
    needs finishing, on the right track?
    Last edited by play_fetch; 03-11-2011 at 04:39 PM.

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    So immediately AFTER I recommend treating strings as strings, you come back with something like code, that deals with chars individually?

    I SO don't understand that. Buffers are just a temporary storage area. Many times, used to hold strings.

  9. #9
    Registered User
    Join Date
    Mar 2011
    Posts
    57
    Quote Originally Posted by Adak View Post
    So immediately AFTER I recommend treating strings as strings, you come back with something like code, that deals with chars individually?

    I SO don't understand that. Buffers are just a temporary storage area. Many times, used to hold strings.
    I know I'm sorry but I tried it couldn't do it and I've latched onto my chars idea now...

    If I get this working, then you can maybe show me how you'd code it with a buffer? I can't expect you to give me the answer to my problem but I also cannot do what you're suggesting because I can't comprehend it.. which is my problem but.. yeah. Silly I know but just... it is how it is.

    I'm getting segmentation fault when using a really long line in file 1

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(int argc, char *argv[])
    {
    	// check num of args
    	if(argc < 4)
    	{
    		fprintf(stderr,"error: not enough arguments\n");	
    	}
    	else
    	{
    		FILE *fileOne =fopen( argv[2], "r");
    		FILE *fileTwo =fopen( argv[3], "r");
    		char *sep = argv[1];
    		if ( fileOne == 0 || fileTwo == 0)
    		{
    			/* error*/
     			fprintf(stderr, "Error: Can't open file");
    			
    		}
    		else	
    		{
    			int i = fgetc(fileOne);
    		        int j = fgetc(fileTwo);
    			while(i!=EOF || j!=EOF)
    			{
    				while((i != '\n') && (i != EOF))
    				{
    					putchar(i);
    					i = fgetc(fileOne);	
    				}
    				printf("%s", sep);
    				while((j != '\n') && (j != EOF))
    				{
    					putchar(j);
    					j = fgetc(fileTwo);
    				}
    			}
    								
    
    
    		}	
    	}
    		return 0;
    }
    Last edited by play_fetch; 03-11-2011 at 05:00 PM.

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Is this a thumbnail version of the job?

    You have lines of text of varying length. You want to append a space, and a second line, onto the first line of text, but only if line1 is not the same as line2.

    Is that right?

    There's a story of a guy out hunting for rabbits. He's got a hunting dog, and a shotgun, and he knows there are rabbits in his fields.

    Crossing a small creek, he walks up the bank and there they are - scores of rabbits, who get scared, and start running all around, including right by him and his startled dog.

    He couldn't aim at any ONE rabbit, because there were so many, all hopping around. He and his dog did not get a rabbit that day.

    You need to stick with someone's advice that you believe makes sense, once you know that they understand the job you want to do. There's always more than one way to do a job.
    Last edited by Adak; 03-11-2011 at 05:13 PM.

  11. #11
    Registered User
    Join Date
    Mar 2011
    Posts
    57
    works fine for normal files, but when I use a big file with 700+ chars in one line it freaks out

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(int argc, char *argv[])
    {
    // check num of args
    	if(argc != 4)
    	{
    		fprintf(stderr,"Usage is %s <number>\n", argv[0]);
    	}
    	else
    	{
    		FILE *fileOne =fopen( argv[2], "r");
    		FILE *fileTwo =fopen( argv[3], "r");
    		char *sep = argv[1];
    		if ( fileOne == 0 || fileTwo == 0)
    		{
    			/* error*/
     			fprintf(stderr, "Error: Can't open file");
    			
    		}
    		else	
    		{
    			int i = fgetc(fileOne);
    		        int j = fgetc(fileTwo);
    			while(i!=EOF || j!=EOF)
    			{
    				while((i != '\n') && (i != EOF))
    				{
    					putchar(i);
    					i = fgetc(fileOne);	
    				}
    				printf("%s", sep);
    				while((j != '\n') && (j != EOF))
    				{
    					putchar(j);
    					j = fgetc(fileTwo);
    				}
    			}
    								
    
    
    		}	
    	}
    		return 0;
    }
    Quote Originally Posted by Adak View Post
    Is this a thumbnail version of the job?

    You have lines of text of varying length. You want to append a space, and a second line, onto the first line of text, but only if line1 is not the same as line2.

    Is that right?
    I want to just concatinate the line from file 1 with a separator string and the line from file two for all the lines in the file, and if there is no line for one of the files but for another just continue to print the sep and the line that exists.

    Atm, it is working for a file with 116 chars, but not with about 700 chars in 1 line. I get a segmentation fault.. no idea what the problem is.

    Quote Originally Posted by Adak View Post

    Crossing a small creek, he walks up the bank and there they are - scores of rabbits, who get scared, and start running all around, including right by him and his startled dog.

    He couldn't aim at any ONE rabbit, because there were so many, all hopping around. He and his dog did not get a rabbit that day.

    You need to stick with someone's advice that you believe makes sense, once you know that they understand the job you want to do. There's always more than one way to do a job.
    The reason I did not follow your advice is because I could not make sense of it, not because it didn't make sense. I hope you are not offended. In programming I know that there are lots of different ways of doing things each with their own advantages...
    Last edited by play_fetch; 03-11-2011 at 05:17 PM.

  12. #12
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Check the value of argv[1] and sep.

    Are i and j being declared anew every time through the loop?

    Depends on your compiler, but I would move variable declarations to the very top of the function, to be sure.

    You're getting no error message at all, and no warnings either?

  13. #13
    Registered User
    Join Date
    Mar 2011
    Posts
    57
    Quote Originally Posted by Adak View Post
    Check the value of argv[1] and sep.

    Are i and j being declared anew every time through the loop?

    Depends on your compiler, but I would move variable declarations to the very top of the function, to be sure.

    You're getting no error message at all, and no warnings either?
    you mean just move the int i; int j; to the top? then assign later? - DONE and still throws error

    no nothing just segmentation fault error msg

    FIXED IT

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(int argc, char *argv[])
    {
    // check num of args
    	if(argc != 4)
    	{
    		fprintf(stderr,"Usage is %s <number>\n", argv[0]);
    	}
    	else
    	{
    		FILE *fileOne =fopen( argv[2], "r");
    		FILE *fileTwo =fopen( argv[3], "r");
    		char *sep = argv[1];
    		int i, j;
    		if ( fileOne == 0 || fileTwo == 0)
    		{
    			/* error*/
     			fprintf(stderr, "Error: Can't open file");
    			
    		}
    		else	
    		{
    			
    			while(i!=EOF || j!=EOF)
    			{
    				i = fgetc(fileOne);
    		        	j = fgetc(fileTwo);
    				while((i != '\n') && (i != EOF))
    				{
    					putchar(i);
    					i = fgetc(fileOne);	
    				}
    				if(i == '\n') i = fgetc(fileOne);
    				printf("%s", sep);
    				while((j != '\n') && (j != EOF))
    				{
    					putchar(j);
    					j = fgetc(fileTwo);
    				}
    				if(j == '\n') j = fgetc(fileTwo);
    				putchar('\n');
    			}
    								
    
    
    		}	
    	}
    		return 0;
    }
    Last edited by play_fetch; 03-11-2011 at 06:06 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to make this work =(
    By readytogo in forum C Programming
    Replies: 1
    Last Post: 10-28-2010, 09:59 AM
  2. Football team league need help to make this work
    By wurzull in forum C Programming
    Replies: 1
    Last Post: 04-07-2007, 07:35 AM
  3. how do I make fstream's >> work for my class?
    By MathFan in forum C++ Programming
    Replies: 3
    Last Post: 04-25-2005, 12:01 PM
  4. Listview sorting...Can't make it work!
    By knutso in forum Windows Programming
    Replies: 4
    Last Post: 10-05-2003, 12:48 PM
  5. how do you make the X Y thing work?
    By bluehead in forum C++ Programming
    Replies: 2
    Last Post: 11-05-2001, 04:19 PM