Quantcast
Channel: Active questions tagged gcc - Stack Overflow
Viewing all articles
Browse latest Browse all 22042

How do I write command line arguments into a file in C without writing (null)? [closed]

$
0
0

I need to output the arguments passed to a program into a file. Every time I do it, it outputs (null) at the end of every line.

This is what I have got:

#include <stdio.h>int main(int argc, char *argv[]) {    FILE *file = fopen("file.txt", "a+");    for (int i = 1; i <= argc; i++) {        fprintf(file, " %s", argv[i]);    }    fputs("", file); // add newline at the end    fclose(file);}

When I read the file with cat I can see (null) at the end of lines. Example:

 asd def (null) second test (null)

Viewing all articles
Browse latest Browse all 22042

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>