Thread: What's wrong with my code?

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    40

    Question What's wrong with my code?

    Hi,

    Sorry if this is a silly question, but I am fairly new to C programming.

    I have written two simple programs (using microsoft visual studio express 2008), which should allow me to input some text and then the program should duplicate what I typed twice and write that to a textfile which it will save on my computer. I expect the two codes to produce the same result but that is not what is happening when I tried them out. What am I doing wrong in the code #2 (which is the one giving me the issue)?


    code #1
    Code:
    #include "stdafx.h"
    
    #include <stdio.h>
    #include <string.h>
    
    int main()
    {
    	FILE *f;
    	FILE *g;
    	FILE *h;
        char s[1000];
       	gets (s);
      	 	 f=fopen("output.txt","w");
    		 g=fopen("output.txt","a");
    		 h=fopen("output.txt","a");
    
        if (!f ||!g || !h)
    		return 1;
    	fprintf(f,"%s", s);
    	fprintf(g,"%s", s);
    	fprintf(h,"%s", s);
    	fclose(f);
    	fclose(g);
    	fclose(h);
    	    return 0;
    }
    code #2
    Code:
    #include "stdafx.h"
    
    #include <stdio.h>
    #include <string.h>
    
    int main()
    {
       	FILE *f;
    	char s[1000];
    	int k, i;
    		
    	gets(s);
    	k = strlen(s);
    	
    	f=fopen("output.txt","w");
    	if (!f)
    		return 1;
    	for (i=0; i<=k; i++)
    	{
    		s[i+k]=s[i];
    	}
    	fprintf(f,"%s\n",s);
    	fclose(f);
    	return 0;
    }
    Thanks!
    Last edited by x2x3i5x; 09-28-2009 at 12:22 AM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Strings in C need to be null terminated. Since you overwrite the first null terminator, you get to add on to your string; so that's fine. BUT, you need to put the null terminator back on the end of your string when you are finished.

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Also, don't ever use 'gets', it's a very common cause of buffer overflows - use 'fgets' instead. And you can probably do away with the stdafx.h include.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    strlen() returns the number of char's in the string *NOT COUNTING* the end of string char ('\0').

    So your program is overwriting the end of string char, and now you don't have a string (or two or three), you have just a long bunch of char's, with no strings.

    Try (i+k+1), and see how that does.

    That should take care of the first and second strings. But it won't take care of the third one. Your loop resets i to zero, and k is still set at the strlen(), so the third string will overwrite, the second string.

    You can do what you want with one loop, but you need to introduce another variable which will set up the third string's write to the char array s. I prefer to use two nested loops. Something like:


    Code:
    for(i = 0; i < 3; i++) {  
       n = i * k + 1;
       for(j = n; j < 3 * n; j++) {
          //etc.
    Code:
    int main()
    {
       	FILE *f;
    	char s[1000];
    	int k, i;
    		
    	gets(s);
    	k = strlen(s);
    	
    	f=fopen("output.txt","w");
    	if (!f)
    		return 1;
    	for (i=0; i<=k; i++)
    	{
    		s[i+k]=s[i];
    	}
    	fprintf(f,"%s\n",s);
    	fclose(f);
    	return 0;
    }
    This is all off the cuff, and not meant as a running program snippet. I'm sure you'll find a better way to do it, in a bit.

    Welcome to the forum - if you're still stumped, answer back and I'll take it farther.

  5. #5
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    I think tabstop's suggestion should work in this case. Just keep the loop as is and null terminate when done. Incidentally, this should really be a function (think: 'strcat'). That clarifies the intent of the program, besides making it more modular. Just seek to the null terminator of the string, write the new characters, and then add a new terminator. Making sure that there is enough room beforehand, of course.

  6. #6
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Code:
    	for (i=0; i<=k; i++)
    	{
    		s[i+k]=s[i];
    	}
    	fprintf(f,"%s\n",s);
    Instead of doing that (which appears to be dangerous since no buffer sizes are checked), why not do this instead?
    Code:
    fprintf(f,"%s",s);
    fprintf(f,"%s\n",s);
    bit∙hub [bit-huhb] n. A source and destination for information.

  7. #7
    Registered User
    Join Date
    Sep 2009
    Posts
    40
    @ Adak

    I was just seeing if I could get this to work since I'm still working my way into the C programming; I think later on when I'm comfortable with C programming, I should be able to write a better code that achieve same result.

    Is the i+k+1 thing you mention supposed to be accounting for the NULL character that comes at end of finished strings? and why i*k+1 later?

    @bithub
    your suggestion seems to help....
    Last edited by x2x3i5x; 09-28-2009 at 02:53 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. what is wrong in this simple code
    By vikingcarioca in forum C Programming
    Replies: 4
    Last Post: 04-23-2009, 07:10 AM
  2. what is wrong with this code please
    By korbitz in forum Windows Programming
    Replies: 3
    Last Post: 03-05-2004, 10:11 AM
  3. I cant find what is wrong with this code
    By senegene in forum C Programming
    Replies: 1
    Last Post: 11-12-2002, 06:32 PM
  4. Anyone see what is wrong with this code?
    By Wise1 in forum C Programming
    Replies: 2
    Last Post: 02-13-2002, 02:01 PM
  5. very simple code, please check to see whats wrong
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 10-10-2001, 12:51 AM