Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How can I read content from a file and use it as input for an ELF file #1656

Closed
AfalpHy opened this issue Apr 28, 2024 · 4 comments
Closed

Comments

@AfalpHy
Copy link

AfalpHy commented Apr 28, 2024

I have written a test program as follows:

#include <stdio.h>

int main() {
    int a = 0;
    scanf("%d", &a);
    printf("output:%d\n", a);
    return 0;
}

I compiled it using the commandclang -target riscv64-linux-gnu -static test.c and executed the spike /riscv64-linux-gnu/bin/pk a.out command. Then, I manually entered 100, and it outputted the correct result 100, just like this:

$clang -target riscv64-linux-gnu -static test.c
$spike /riscv64-linux-gnu/bin/pk a.out
100
output:100

But when I used another command spike /riscv64-linux-gnu/bin/pk a.out < input.txt(the content of input.txt is 100), it didn't output the expected 100 but instead 0, just like this:

$spike /riscv64-linux-gnu/bin/pk a.out < input.txt
output:0

Is there an error in my usage? What should I do?

@jerryz123
Copy link
Collaborator

fd-0 (stdin) is redirected to the ns16550 UART device model, so piping data into spike via stdin will not work.

A fopen in your target program will work.

@AfalpHy
Copy link
Author

AfalpHy commented May 14, 2024

Thanks for your reply.
I'm sorry that my question misled you. I don't want to change my test program, in other words, I don't want to use fopen. How can I pass the contents of a file to my program? Is it possible to achieve this by modifying the shell command(spike /riscv64-linux-gnu/bin/pk a.out < input.txt)?

@jerryz123
Copy link
Collaborator

No. Passing the contents of a file to your program is best done with fopen.

@AfalpHy
Copy link
Author

AfalpHy commented May 15, 2024

OK. Thank you again for your reply.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants