gpt4 book ai didi

c++ - C 程序的执行方式与 shell 不同

转载 作者:行者123 更新时间:2023-11-30 20:51:23 26 4
gpt4 key购买 nike

我试图将此输入作为字符读入 c 中的二维数组内存中。

00P015
00P116
030000
06P0ZZ
030005
06P1ZZ
04P0ZZ
26P1ZZ
3412ZZ
030010
06P0ZZ
99ZZZZ
030010
06P1ZZ
99ZZZZ
ZZ0000
ZZ0010

我的代码是

#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>


int pr;
int value;
/*These are variables representing the VM itself*/

char IR[6] ;
short int PC = 0 ;

int P0 ; //these are the pointer registers
int P1 ;
int P2 ;
int P3 ;

int R0 ; //GP regs
int R1 ;
int R2 ;
int R3 ;

int ACC ;
char PSW[2];
char memory [100][6] ; //this is the program memory for first program
short int opcode ; //nice to know what we are doing
int program_line = 0 ;


int fp ;
int i ;
int q = -1; //Used to iterate through memory to execute program
int TrueFalse; //True / False value for check statements, 1 implies true, 0 implies false
int halt = 0;
int address;


char input_line [7] ;

main(int argc, char *argv[])
{ //Read file into VM
fp = open("C:\\Users\\Whiskey Golf\\ClionProjects\\untitled\\program.txt", O_RDONLY) ;
printf("Open is %d\n", fp) ; //always check the return value.

if (fp < 0) //error in read
{printf("Could not open file\n");
exit(0) ;
}
//read in the first line of the program
int charRead = read (fp, input_line, 8 ) ; //returns number of characters read`
printf("\n*******************************\n");
printf("* Reading Program Into Memory *\n");
printf("*******************************\n");
while (1)
{ if (charRead <= 0) //indicates end of file or error
break ; //breaks out of infinite loop

for (i = 0; i < 6 ; i++) //If we get here must have correctly read in a line of program code.
memory[program_line][i] = input_line[i] ; //copy from input line into program memory

printf("Program Line %d: ", program_line) ; //printing out program line for debugging purposes
for(i = 0; i < 6; i++)
printf("%c", memory[program_line][i]) ;
printf("\n") ;

opcode = (memory[program_line][0] -48) *10 ; //Get opcode, print out opcode to console
opcode += (memory[program_line][1] -48) ;

printf("Opcode is %d\n", opcode) ;

charRead = read (fp, input_line, 8) ; //read in next line of code

if(input_line[0] == 'Z') //if the firat character is a 'Z' then you are reading data.
break ; //No more program code so break out of loop

program_line++ ; //now at a new line in the prog
printf("%n");
}

我遇到的问题是,当我在 IDE 中运行我在 Clion 中编写的程序时,我的输出是正确的,我得到

Program Line 0: 00P015
Opcode is 0
Program Line 1: 00P116
Opcode is 0
Program Line 2: 030000
Opcode is 3
Program Line 3: 06P0ZZ
Opcode is 6

但是当我通过 gcc 编译通过 shell 运行代码然后执行 ./a.out 时,我得到的输出是

Program Line 0: 00P015
Opcode is 0
Program Line 1: 16
Opcode is -528
Program Line 2: 00
Opcode is -528
Program Line 3: ZZ
Opcode is-528

我已经尝试调试这个问题有一段时间了,当我通过 shell 执行此操作时,我无法让它正常工作,而这正是我需要执行的方式。任何帮助将不胜感激。

最佳答案

您正在读取 8 字节,该字节采用行尾字符 '\n' 并尝试将其存储在 7 字节数组中.

read (fp, input_line, 8)

这会导致未定义的行为,它应该是

read(fp, input_line, 7)

然后你可以像这样丢弃下一个字节

char discard; 
read(fp, &discard, 1);

我想您正在读取 8 字节来消耗行尾字符,因此您可以将数组大小增加到 8 并忽略最后一个字符或简单地读取并丢弃它。

编辑:仔细查看数据和代码,我发现我不明白你想要做什么,你必须只读取 7 个字符,将包含尾随 '\n',当且仅当每行后面始终有一个新行 '\n' 时,以下代码才会起作用,否则它将跳过最后一行,你应该自己想出明显的解决方案。另请参阅this comment ,如果你在MS Windows上用文本编辑器编写程序,你就会遇到麻烦。要解决这个问题,您可以使用 fopen() 而不是低级 I/O。

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>

int
main(void)
{
int file;
ssize_t length;
char buffer[7];
file = open("program.txt", O_RDONLY);
if (file == -1)
return -1;
while ((length = read(file, buffer, sizeof(buffer))) == 0)
{
int opcode;
/* You will need to overwrite the '\n' for the printf() to work
* but you can skip this if you don't want to print the command
*/
buffer[length - 1] = '\0';
opcode = 10 * (buffer[0] - '0') + buffer[1] - '0';
fprintf(stderr, "Command: `%s'\n\topcode: %d\n", buffer, opcode);
}
close(file);
return 0;
}

关于c++ - C 程序的执行方式与 shell 不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32772630/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com