×

C Tutorial

C Basics

C Data Types

C Input/Output

C Operators

C Conditional Statements

C Control Statements

C Strings

C Functions

C Arrays

C Structure and Unions

C Pointers

C Preprocessor Directives

C Command-line Arguments

C File Handlings

C Graphics

C Advance Topics

C Tips and Tricks

C Important Topics

C Practice

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

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.

Example

program - main.c

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

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.

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.



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.