简体   繁体   中英

Cant read file name when Input through cmd in C ( through args[1] )

This is how I am reading my text files in cmd. It is a constraint.

>gcc -std=c99 myproject.c -o myproject
>myproject<1.txt

What I'm trying to achieve is the file name "1.txt" as a string so that I can read the number for lines in the file. I looked through a bunch of solutions and all of them suggest argv[1] but when I do this:

int main(int args, char*argv[]) {
printf("File: %s\n", argv[1]); }

it prints null.

args gives 1 and argv[0] gives myproject. I don't know what else I could try. All I want to achieve the number of lines in the given input file. Please help. Thanks!

Using redirection (<) gives the data to the program on the standard input (the program sees the data just as if they were typed in by the keyboard), if you want to pass just the parameter simply delete the redirection.

myproject 1.txt

... if you just want to 'count lines' give a try to the command wc -l , if it's a homework, just check you're dealing with the file in the correct way: Use fscanf, fgetc, fputs, fputc, ... instead of the 'standard input' ones scanf, gets, getc, putc, ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM