1

I'm writing a simple exercise app in C, which should ask user if he want write something to file or read the file. I want give name to the file and then input some text in console. I want use that text to save it to the file.

The problem is that, when I already give name to the file, I can't input data to the console and by this I can't save these datas to the text file. In addition, the while loop ignores my 'y' to restart the program again. Furthermore, when I want use reading file then it really does, program works, but it also adds instruction from default (printing just error) but I don't want that, just only read from file and print it to the console.

Can someone explain me what I'm doing wrong and how to solve this? I'd be grateful.

Here's my code:

int main()
{
FILE *file;
char nameFile[32];
char string[500];
char ans[2];
int choice;

do{
    printf("What do you want to do?\n");
    printf("1. Write text to file\n");
    printf("2. Read text file\n");
    scanf("%d", &choice);
    switch (choice) {
        case 1:
            printf("Give name to the file (*.txt).\n");
            scanf("%s", nameFile); //This line skips me to line where program ask user if restart the program (I have no idea why :()
            system("clear");
            file=fopen(nameFile, "w");
            if(file!=NULL){
                printf("Input text:\n");
                scanf("%[^\n]", string); //<--- HERE I'cant input text to console, seems like scanf doesn't work.
                fprintf(file, "%s", string);
                printf("\n\t\t\t-----------Ended writing.------------\n");
                fclose(file);
            }
            else
            {
                printf("Could not open the file.");
                return -1;
            }
            break;
        case 2:
            printf("Give name to the file (*.txt)");
            scanf("%s", nameFile);
            system("clear");
            file=fopen(nameFile, "r");
            if(file!=NULL){
                while (!feof(file)) {
                    fscanf(file, "%s", string);
                    printf("%s\n",string); //After printing data from text file, adds instruction from line , and that is printing Error. How to get rid of it?
                }
            }
            else{
                printf("Could not open the file.");
                return -1;
            }
        default:
            printf("Error.");
            break;
    }
    printf("Do you want to restart the program? (y/*)"); //Even if I write 'y', program ends anyway :(
    scanf("%s", ans);
}
while(ans=='y');
return 0;
}

https://ideone.com/aCYJR5

Sisimośki
  • 99
  • 1
  • 9

2 Answers2

1

your while should be

 while(ans[0]=='n');

A char array is a string, you are trying to compare a whole string to the character 'y'. Also, it should "Loop while ans[0] is equal to 'n'" meaning keep going if the user specifies n.

Katianie
  • 547
  • 1
  • 9
  • 31
1

scanf("%[^\n]", string); doesn't work unless the input buffer is clear. Use instead scanf("%499s", string) where 499 is the size of the string minus 1.

Don't use while (!feof(file)){...}, use instead while(fscanf(file, "%500s", string) == 1){...}

Use while(ans[0]=='y') as suggested earlier. Or use

char ans;
scanf(" %c", &ans);//note the blank space before `%c`
while(ans == 'y')

You also forgot to break a switch statement. Try the following:

char c;
char ans;
do {
    printf("What do you want to do?\n");
    printf("1. Write text to file\n");
    printf("2. Read text file\n");
    scanf("%d", &choice);

    switch(choice) {
    case 1:
        printf("Give name to the file (*.txt).\n");
        scanf("%s", nameFile);
        file = fopen(nameFile, "w");
        if(file == NULL) { printf("Could not open the file."); return -1; }
        printf("Input text:\n");

        while((c = getchar()) != '\n' && c != EOF);
        fgets(string, sizeof(string), stdin);
        string[strcspn(string, "\r\n")]=0;

        fprintf(file, "%s", string);
        printf("\n\t\t\t-----------Ended writing.------------\n");
        fclose(file);
        break;
    case 2:
        printf("Give name to the file (*.txt)");
        scanf("%s", nameFile);
        file = fopen(nameFile, "r");
        if(file == NULL) { printf("Could not open the file."); return -1; }
        while(fgets(string, sizeof(string),file))
            printf("%s\n", string);
        fclose(file);
        break;
    }
    printf("Do you want to restart the program? (y/*)");
    scanf(" %c", &ans);
} while(ans == 'y');

See also
Why is “while ( !feof (file) )” always wrong?
How to clear input buffer in C?

Barmak Shemirani
  • 26,741
  • 4
  • 33
  • 61
  • Thank you, it definitely works, but what I have to do if I want to write text to file with whitespaces? With simple "%s", or as you suggested "%499s", it really shows me and then I can input the text, but it's missing my words after space. What should I do to write text to file with whitespaces? – Sisimośki Nov 28 '17 at 07:37
  • In that case you need to clear the buffer and use `fgets`, see the links I added. You also need `fgets` to read the file one line at a time. See edit. – Barmak Shemirani Nov 28 '17 at 07:59