1

I need to be able to prompt the user to input text, which is then taken and inputed into a text file. It then stops reading once the user enters CRTL-D/fputs returns NULL.

I can open a file and use fprintf to write to it, but now I need to write to the file using user input. I don't really know where to start.

I know how to read a .txt file and print its contents to the terminal using fputs, but writing to one through the terminal is too confusing for me...

int main(int argc, char *argv[]) {
FILE *f;

  f = fopen(argv[1], "w");
  if (errno) {
      perror("Error opening file.");
      exit(1);
  }

  fprintf(f, "Hello, Jake.\n");
  fclose(f);
}

I need a while loop that ends once the fputs or feof returns NULL.

If at any point I have said something that doesn't make sense, it's because I am confused. Learning C for uni is driving me nuts :(

Jake Jackson
  • 1,055
  • 1
  • 12
  • 34
  • 1
    *I need to write to the file using user input. I don't really know where to start.* - Start with reading user input? – Eugene Sh. Oct 24 '19 at 14:45
  • https://stackoverflow.com/questions/8115218/writing-user-input-to-a-file-in-c – Ayush Oct 24 '19 at 15:01

1 Answers1

1

You should create and open the file first. Then in a loop, start reading input with fgets(). In every iteration, write what you just read to the file. When the user inputs EOF, then close the file and you are done.

Example:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define BUFFERSIZE 64

int main(void)
{

    FILE *fp;   /* File pointer*/
    char filename[] = "output.txt";

    /* Creating (open) a file*/
    fp = fopen(filename, "w");
    /* Check file created or not*/
    if(fp == NULL)
    {
        printf("File was not created!!!\n");
        exit(0); /* Exit from program*/
    }

    printf("File created successfully\n");
    /* Read from input */
    char buffer[BUFFERSIZE];
    while(fgets(buffer, BUFFERSIZE, stdin)) /* Break with ^D or ^Z */
    {
        /* Remove trailing newline */
        buffer[strcspn(buffer, "\n")] = 0;
        /* Writting into file*/
        fprintf(fp, "%s\n", buffer);
    }

    printf("Data written successfully.\n");
    fclose(fp);
    return 0;
}

Output:

Georgioss-MBP:gsamaras$ gcc main.c
Georgioss-MBP:gsamaras$ ./a.out 
File created successfully
Hello Stack Overflow
You are the best!
Data written successfully.
Georgioss-MBP:gsamaras$ cat output.txt 
Hello Stack Overflow
You are the best!

Note: I removed the newline fgets stores.

gsamaras
  • 71,951
  • 46
  • 188
  • 305
  • 1
    Thanks, man. Could you explain what is going on inside the while loop? I understand the strcspn function, but not quite what it means in conjunction with the rest of the code in the loop. – Jake Jackson Oct 24 '19 at 15:50
  • You are welcome @JakeJackson. Read the note at the bottom of my answer to understand the removal of the newline (it basically just finds the occurrence of the newline, indexes then the buffer at that position, and assigns the NULL terminator (well a 0 here, but it's a synonym in that case) there). Now the `fprintf(fp, "%s\n", buffer);` call says to write the contents of `buffer` as a string, and then append a newline, exactly as you would do to print in the screen (stdout), but now, instead of sending the output to the stdout, you send it to the file pointed by the file pointer `fp`. – gsamaras Oct 24 '19 at 16:20
  • Ah, I hadn't noticed that link. Again, thanks a lot for your help. It makes sense once I see some code which I can't start tinkering with. – Jake Jackson Oct 24 '19 at 18:19