0

I am fairly new to programming and i have done some in school, but they only taught basic functions like printf, scanf, for, while, pointers. I am making a small program that will print user input in to a file, but when the text is written into the file the first letter of every word after the first is missing and i don't know why. can somebody explain what is happening to the first letters and do so in a simple manner if you can, please and thank you for your answer.

This is the function i am using to write to the file.

void text(){
    int e=1;        
    puts("After a sentance press enter to continue or esc to stop");` 
    printf("Enter text now\n");
    FILE *fp;
    fp=fopen("Text.txt","w");
End:while(e==1){
    char txt[100];
    puts(gets(txt),fp);
    if(getche()=='\e')
    e=0;
    goto End;   
    }   //end of while
        fclose(fp);
    }// end of function

New code

void text(){
    int e=1;        
    puts("After ending a sentance press enter to continue or esc to stop"); 
    printf("Enter text now\n");
    FILE *fp;
    fp=fopen("Text.txt","w");
while(e==1){
    char txt[100];
    fgets(txt,100,stdin);
    fwrite(txt,sizeof(char),sizeof(txt),fp);
    if(getche()=='\e'){
        break;
    }
    else;   
    }   
        fclose(fp);
    }
Tim
  • 1
  • 6
  • Did you seriously use `goto`? It's bad practice. It is clear _here_, but still... – Arc676 Nov 06 '15 at 08:39
  • Also, why don't you use `fgets` for input and `fprintf` to write to the file? – Arc676 Nov 06 '15 at 08:40
  • i tried using break; but it didn't break the loop immediately – Tim Nov 06 '15 at 08:41
  • @Tim: `break` breaks the loop immediately, your `goto` does nothing, because you are going back to the beginning of the loop, the same place it would go without the `goto` anyway. – rodrigo Nov 06 '15 at 08:43
  • You mean like this: fprintf(fp,fgets(txt,100,stdin),"%s"); ? Didn't work – Tim Nov 06 '15 at 08:45
  • @Tim try not to use labels –  Nov 06 '15 at 08:46
  • Try to use `fwrite` also –  Nov 06 '15 at 08:48
  • Or if performence is not important for you use `fprintf` –  Nov 06 '15 at 08:49
  • Ok, i made it so that it breaks now insted of using goto, funny thing is i tried it before and it didn't work :) – Tim Nov 06 '15 at 08:51
  • Has your school taught you `gets`? I seriously hope not. See here: http://stackoverflow.com/questions/22419866/is-the-gets-string-function-in-c-considered-a-bad-practice – user4520 Nov 06 '15 at 08:55
  • @szczurcio no they haven't, they only taught printf and scanf for input and output.There were only around 20 hours of class and they wasted half of it. – Tim Nov 06 '15 at 09:16
  • Well, just don't use `gets`. It's so bad it was removed in the latest C revision. Instead, you can use `fgets`. – user4520 Nov 06 '15 at 09:23
  • @Ehsan i tried using fwrite but it turned the text in the file gibberish `fwrite(txt,sizeof(char),sizeof(txt),fp);`. I think i am not using it i the correct way, can someone show me how it would look if i would put it in my code? – Tim Nov 06 '15 at 09:24
  • Bro you should open the file in "wb" flag coz fwrite writes into the file in a binary syntax –  Nov 06 '15 at 09:25
  • And before writing to the file check that the file pointer isn't `NULL` –  Nov 06 '15 at 09:26
  • And instead of sizeof(t) just pass the size of the txt, I mean 100. –  Nov 06 '15 at 09:28
  • coz sizeof(txt) returns the size of a pointer which in new systems is 4 Bytes –  Nov 06 '15 at 09:29
  • @Ehsan: No, `sizeof(txt)` is the size of the variable, that is of type `char[100]`, so `100` bytes. But he should probably use `strlen(txt)` because the string may be shorter. Also the check for `NULL` is useless because a local array cannot be `NULL`. – rodrigo Nov 06 '15 at 09:56
  • idk I have heared it returns the size of pointer I really don't know –  Nov 06 '15 at 09:57

1 Answers1

0

Your code has many issues, but... your particular problem is that getche() reads one character, if it is \e you do something, but if it is another characters, then it is lost!

Using your functions of choice (getche() and gets()) you can do:

while (1)
{
    char c = getche();
    if (c == '\e')
        break;
    txt[0] = c;
    gets(txt+1);
    /* ... */
}

This will not remove the first character, but will fail with empty lines, I think.. I will leave that as an exercise to the reader.

rodrigo
  • 94,151
  • 12
  • 143
  • 190
  • I only need to break the loop with esc(`\e`) so i don't need it if the loop continues or if it breaks and i know i have many problems in the code, but i don't know exactly which commands are better for what job. – Tim Nov 06 '15 at 09:05
  • does `getche()` take the missing letter or where does it end up? – Tim Nov 06 '15 at 09:18
  • @Tim: It returns the read character. You are comparing it with `\e`, but otherwise not storing it. – rodrigo Nov 06 '15 at 09:20
  • you ware right, the first letter is lost because of the `getche`. I will try to take your code and put it in my program, thank you for all your help. – Tim Nov 06 '15 at 14:41