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!