我正在尝试打开一个 .txt 文件并将其内容放入标准输入中。我知道你可以这样做:
myapp < input.txt
Run Code Online (Sandbox Code Playgroud)
但我想在程序中多次使用相同的内容,并且我认为使用这种方法,标准输入内容将被消耗并且不能再次使用。
我想测试一个从标准输入读取的函数,就像我正在尝试的一个例子:
void myFunction(int number)
{
// The function already writen reads from stdin using the argument.
}
void fillStdin(void)
{
FILE* myFile;
myFile = fopen("myfile.txt", "r");
// Put the content of the file in stdin
fclose(myFile);
}
int main(void)
{
int myArray[5] = {1, 2, 3, 4, 5};
for (int i = 0; i < 5; i++)
{
fillStdin();
myFunction(myArray[i]);
}
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我在汇编调用c函数时做了一些测试,当我使用ansi转义代码并调用使用printf的ac函数时,我得到了我认为的奇怪行为.
这是装配部分:
section .data
red db 27,"[31;1m",0
redlen equ $ - red
cyan db 27,"[36;1m",0
cyanlen equ $ - cyan
colorReset db 27,"[0m",0
colorResetLen equ $ - colorReset
section .text
extern printLetter
extern letter
global main
main:
mov BYTE [letter], 'H'
call ansiSetRed
call printLetter
mov BYTE [letter], 'e'
call ansiSetCyan
call printLetter
mov BYTE [letter], 'l'
call ansiReset
call printLetter
mov BYTE [letter], 'l'
call ansiSetRed
call printLetter
mov BYTE [letter], 'o'
call ansiSetCyan
call printLetter
mov BYTE [letter], …Run Code Online (Sandbox Code Playgroud)