Home » C programs

How to save C program's Output in a file in Linux?



Here, we are going to learn how can we save the output of a C program in Linux GCC/G++? Here, we will create a C program and while executing the program, we will save the output in a text file.
Submitted by IncludeHelp, on March 07, 2018

The solution is:
./main > file.txt
[main is binary file name]
Please read complete article to learn better.

Saving C program's Output in a file in Linux

We can write a program and also get the output after executing, but a question arise by many programmers how to save/store program's output into a file in Linux?

Here is a simple method by which you can store program's output into a file and further you can use that file for your reference.

Let's understand with an example: program to print your name 100 times.

program - main.c

#include <stdio.h>
 
int main()
{
    char name[]="Mike";
    int i;
     
    for(i=0;i<100;i++)
        printf("%s ",name);
     
    return 0;
}

Now, Compile and Run the program,

To compile:

gcc main.c –o main

To run:

./main

Output:

sh-4.3$ gcc main.c -o main
sh-4.3$ ./main
Mike Mike Mike Mike Mike Mike Mike Mike Mike Mike Mike Mike Mike Mike Mike 
Mike Mike Mike Mike Mike Mike Mike Mike Mike Mike Mike Mike Mike Mike Mike 
Mike Mike Mike Mike Mike Mike Mike Mike Mike Mike Mike Mike Mike Mike Mike 
Mike Mike Mike Mike Mike Mike Mike Mike Mike Mike Mike Mike Mike Mike Mike 
Mike Mike Mike Mike Mike Mike Mike Mike Mike Mike Mike Mike Mike Mike Mike 
Mike Mike Mike Mike Mike Mike Mike Mike Mike Mike Mike Mike Mike Mike Mike 
Mike Mike Mike Mike Mike Mike Mike Mike Mike Mike

But, you cannot store this output longer, as you run program again or close the terminal.

Then, how to store the output?

This is a simple command to store output - not only for c programs even you can store any terminal command's output.

The command syntax is:

terminal_command > file_path

The command is:

./main > file1.txt

Yes, by adding greater than (>) and target file name you can store output in this file.

Run the command (Compile, Run and CAT command to read file) - Output

sh-4.3$ gcc main.c -o main
sh-4.3$ ./main > file.txt 
sh-4.3$ cat file.txt
Mike Mike Mike Mike Mike Mike Mike Mike Mike Mike Mike Mike Mike Mike Mike 
Mike Mike Mike Mike Mike Mike Mike Mike Mike Mike Mike Mike Mike Mike Mike 
Mike Mike Mike Mike Mike Mike Mike Mike Mike Mike Mike Mike Mike Mike Mike 
Mike Mike Mike Mike Mike Mike Mike Mike Mike Mike Mike Mike Mike Mike Mike 
Mike Mike Mike Mike Mike Mike Mike Mike Mike Mike Mike Mike Mike Mike Mike 
Mike Mike Mike Mike Mike Mike Mike Mike Mike Mike Mike Mike Mike Mike Mike 
Mike Mike Mike Mike Mike Mike Mike Mike Mike Mike

Output is stored in file.txt and you can open and read the output from the file using cat command.






Was this page helpful? YES NO

Are you a blogger? Join our Blogging forum.



Comments and Discussions





© https://www.includehelp.com (2015-2018), Some rights reserved.




close Like other websites, this site uses cookies to deliver relevant ads based on your interest, by using our website, you acknowledge that you have read our privacy policy.