← Back to list

Easy way to read input in c and c++ — part 6

We were here:

Doritasci · 2025-07-28 14:24 · 0 claps · 3.6 min read
#scanf #c-language-for-beginners #linux-input-operator #appending-string #write-to-file
Open on Medium ↗
Wiki topics: 🔓 · Open Source

Easy way to read input in c and c++ — part 6

We were here:

//--
//c
//test2w.c
#include <stdio.h>

int main(int argc, char *argv[])
{
    char *path = (argc > 2) ? argv[2] : "outputw.txt"; // if argv[2] is not indicated get outputw.txt for default

    FILE *fptr = fopen(path, "w");

    if (!fptr) 
    {
        perror("Error creating file ");
        return 1;
    }
    else
    {
        printf("File successfully opened %s\n", path);
    }  

    if (fputs(argv[1], fptr) == EOF)     //- Writes argument to file.
    {
        perror("EOF ");
        return 1;
    }
    else
    {
        printf("Data successfully written in file %s\n", path);
    }    

    if (fclose(fptr))  //- Close file
    {
        perror(path);
        return 1;
    }
    else
    {
        printf("The file is now closed.\n");
    }
    return 0;
}
//--

Terminal:

$ gcc test2w.c -o test2w
$ ./test2w argonauts outputw.txt 
File successfully opened outputw.txt
Data successfully written in file outputw.txt
The file is now closed.

File outputw.txt:

argonauts

This code is used to access a text file, write to it, close it and handle some types of errors. However, we have a problem: every time the program writes to the file, it deletes the previous content. Now we try to fix that problem. The solution is:

    //FILE *fptr = fopen(path, "w"); // <-- from this, "write"
    FILE *fptr = fopen(path, "a");   // <-- to this, "append"

Let’s test it:

$ gcc test2w.c -o test2w
$ ./test2w "sailed to Colchis to find the Golden Fleece"
File successfully opened outputw.txt
Data successfully written in file outputw.txt
The file is now closed.
$ 

File outputw.txt:

argonauts
sailed to Colchis to find the Golden Fleece

Done! Now, something for Linux users:

//--
//c
//test2w.c
#include <stdio.h>

int main(int argc, char *argv[])
{
    char *path = "outputw.txt";
    char name[20];
    scanf("%s\n", name);
    printf("Hello, %s!\n", name);

    FILE *fptr = fopen(path, "a");

    if (!fptr) 
    {
        perror("Error creating file ");
        return 1;
    }
    else
    {
        printf("File successfully opened %s\n", path);
    }  

    if (fputs(name, fptr) == EOF)     //- Writes argument to file.
    {
        perror("EOF ");
        return 1;
    }
    else
    {
        printf("Data successfully written in file %s\n", path);
    }    

    if (fclose(fptr))  //- Close file
    {
        perror(path);
        return 1;
    }
    else
    {
        printf("The file is now closed.\n");
    }
    return 0;
}
//--

Ok! Code is done, now I make a file named oust.txt and I write Jason. After that:

$ ./test2w < oust.txt
Hello, Jason!
File successfully opened outputw.txt
Data successfully written in file outputw.txt
The file is now closed.
$

The content of outputw.txt is, now:

argonauts
sailed to Colchis to find the Golden Fleece
Jason

Fantastic! Not exactly because if the content of oust.txt is:

son of Aeson, the rightful king of Iolcos

The function scanf() get all until first space, a widely recognized problem. So we have:

argonauts
sailed to Colchis to find the Golden Fleece
Jason
son

How can I do, then? In this way:

//--
//c
//test2w.c
#include <stdio.h>

int main(int argc, char *argv[])
{
    char *path = "outputw.txt";
    //char name[20];
    //scanf("%s\n", name);
    char name[43];                   //<--- appropriate size
    scanf("%[^\n]s",name);           //<--- it is a way
    printf("Hello, %s!\n", name);

    FILE *fptr = fopen(path, "a");

    if (!fptr) 
    {
        perror("Error creating file ");
        return 1;
    }
    else
    {
        printf("File successfully opened %s\n", path);
    }  
    if (fputs(name, fptr) == EOF)     //- Writes argument to file.
    {
        perror("EOF ");
        return 1;
    }
    else
    {
        printf("Data successfully written in file %s\n", path);
    }    

    if (fclose(fptr))  //- Close file
    {
        perror(path);
        return 1;
    }
    else
    {
        printf("The file is now closed.\n");
    }
    return 0;
}
//--

Terminal:

$ ./test2w < oust.txt
Hello, son of Aeson, the rightful king of Iolcos!
File successfully opened outputw.txt
Data successfully written in file outputw.txt
The file is now closed.

File outputw.txt:

argonauts
sailed to Colchis to find the Golden Fleece
Jason
son
son of Aeson, the rightful king of Iolcos

But if I insert the data myself:

$ ./test2w
Jason married Medea.
Hello, Jason married Medea.!
File successfully opened outputw.txt
Data successfully written in file outputw.txt
The file is now closed.
$

File outputw.txt:

argonauts
sailed to Colchis to find the Golden Fleece
Jason
son of Aeson, the rightful king of IolcosJason married Medea.

The text id badly formatted and we know why. It’s because the methods to read data from stdin (standard input stream) are different: once is from file, other one is from keyboard. All right, I’ll remember that. See it again:

$ ./test2w
gone wild
Hello, gone wild!
File successfully opened outputw.txt
Data successfully written in file outputw.txt
The file is now closed.

File outputw.txt, now:

argonauts
sailed to Colchis to find the Golden Fleece
Jason
son of Aeson, the rightful king of IolcosJason married Medea.
gone wild

And again:

$ ./test2w < oust.txt
Hello, "son of Aeson, the rightful king of Iolcos"!
File successfully opened outputw.txt
Data successfully written in file outputw.txt
The file is now closed.
$

File outputw.txt:

argonauts
sailed to Colchis to find the Golden Fleece
Jason
son of Aeson, the rightful king of IolcosJason married Medea.
gone wildson of Aeson, the rightful king of Iolcos

Well! I stop here, for now. I think that materials above are sufficient to test some tasks for the curious amongst us. In the next article we will see something more. Thank you for staying until here. See you soon.


메타데이터
post_id
961069aff5a2
slug
easy-way-to-read-input-in-c-and-c-part-6-961069aff5a2
url
https://medium.com/@doritasci/easy-way-to-read-input-in-c-and-c-part-6-961069aff5a2
canonical_url
https://medium.com/@doritasci/easy-way-to-read-input-in-c-and-c-part-6-961069aff5a2
author_url
https://medium.com/@doritasci
status
ok
fetched_at
2026-07-18 16:42:02