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

C++ C程序的执行方式与shell不同,c++,c,C++,C,我试图将这个输入作为字符读入c中的二维数组中的内存。 00P015 00P116 030000 06P0ZZ 030005 06P1ZZ 04P0ZZ 26P1ZZ 3412ZZ 030010 06P0ZZ 99ZZZZ 030010 06P1ZZ 99ZZZZ ZZ0000 ZZ0010 我的代码是 #包括 #包括 #包括 #包括 #包括 #包括 #包括 #包括 国际公共关系; int值; /*这些是表示VM本身的变量*/ char-IR[6]; 短整数PC=0; int-P0//这些是

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

00P015
00P116
030000
06P0ZZ
030005
06P1ZZ
04P0ZZ
26P1ZZ
3412ZZ
030010
06P0ZZ
99ZZZZ
030010
06P1ZZ
99ZZZZ
ZZ0000
ZZ0010
我的代码是

#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
国际公共关系;
int值;
/*这些是表示VM本身的变量*/
char-IR[6];
短整数PC=0;
int-P0//这些是指针寄存器
int-P1;
int P2;
int-P3;
int R0//GP注册表
int R1;
int R2;
int R3;
国际会计准则;
char-PSW[2];
字符存储器[100][6]//这是第一个程序的程序存储器
短int操作码//很高兴知道我们在做什么
int program_line=0;
int-fp;
int i;
int q=-1//用于在内存中迭代以执行程序
int-TrueFalse//check语句的True/False值,1表示True,0表示False
int=0;
int地址;
字符输入_行[7];
main(int argc,char*argv[])
{//将文件读入虚拟机
fp=open(“C:\\Users\\whisky Golf\\ClionProjects\\untitled\\program.txt”,仅限O_RDONLY);
printf(“打开的是%d\n”,fp);//始终检查返回值。
if(fp<0)//读取时出错
{printf(“无法打开文件\n”);
出口(0);
}
//读入程序的第一行
int charRead=read(fp,input_行,8);//返回读取的字符数`
printf(“\n***************************************\n”);
printf(“*将程序读入内存*\n”);
printf(“***************************************\n”);
而(1)

{if(charRead您正在读取
8
字节,该字节取行尾字符
'\n'
并尝试将其存储在
7
字节数组中

read (fp, input_line, 8)
这会导致未定义的behavrio,应该是

read(fp, input_line, 7)
然后你可以丢弃下一个字节,就像

char discard; 
read(fp, &discard, 1);
我假设您正在读取
8
字节以使用行尾字符,因此您可以将数组大小增加到
8
并忽略最后一个字符,或者直接读取并丢弃它

编辑:仔细查看数据和您的代码,我发现我不明白您试图做什么,您必须只阅读
7个
字符,其中包括尾随的
'\n'
,如果且仅当每行后始终有新行
'\n'
时,以下代码才有效,否则它将跳过最后一行,你应该自己想一个明显的解决方案。另外,请看,如果你在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;
}
#包括
#包括
#包括
#包括
int
主(空)
{
int文件;
ssize_t长度;
字符缓冲区[7];
文件=打开(“program.txt”,仅限O_rdo);
如果(文件==-1)
返回-1;
而((长度=读取(文件、缓冲区、大小(缓冲区)))==0)
{
int操作码;
/*您需要覆盖“\n”才能使printf()正常工作
*但如果不想打印命令,可以跳过此步骤
*/
缓冲区[长度-1]='\0';
操作码=10*(缓冲区[0]-'0')+缓冲区[1]-'0';
fprintf(stderr,“命令:`%s'\n\topcode:%d\n”,缓冲区,操作码);
}
关闭(文件);
返回0;
}
将8个字节读入7字节数组,这是不好的。它只是在数组之后写入一些内存,但由于数组是7字节,并且大多数数据都是按4或8字节值对齐的,因此您可能会通过不在任何重要的数据上读取数据来逃脱

但是!!!这是你的数据:

00P015<EOL>
00P116<EOL>
030000<EOL>
06P0ZZ<EOL>
030005<EOL>
...
00P015
00P116
030000
06P0ZZ
030005
...
在基于Unix的系统上,行尾是一个字节,读取8个字节将读取

00P015<EOL>0
0P116<EOL>03
00P0150
接下来的八个字节将被读取

00P015<EOL>0
0P116<EOL>03
0P11603
等等…这是你关于药物的数据:

00P015<EOL>0
0P116<EOL>03
0000<EOL>06P
0ZZ<EOL>0300
05<EOL>...
00P0150
0P11603
000006便士
0Z0300
05...
看看会发生什么?不是你需要或想要的

这在IDE中是如何工作的,smurfed,如果我知道的话,除非输入文件实际上是一个windows文本文件(两字节的行尾标记),但它在玩火。我将坚持使用C和pitch作为阅读的替代。我还去掉了本例中不必要的所有内容

#include <string.h>
#include <stdio.h>
#include <stdlib.h>


int main(int argc, char *argv[])
{
    (void) argc; // I'm being pedantic. As pointed out below in the comments, this
                 // is not needed. Main needs no arguments. I just like them.
    (void) argv;
    //Read file into VM
// moved all variables into function
    char memory [100][6] ; //This is likely program death if you read more than 100 
                           // lines from the file. There are no guards to prevent this 
                           // in the original code.
    int opcode ;
    int program_line = 0 ;


    FILE* fp ; //using a C FILE handle rather than a posix handle for maximum portability

    char input_line [8] ;// death if a line is poorly formatted and has extra characters, 
                         // but in this case, the whole program falls apart.                            
                         // Went with 8 in case the input file was formatted for Windows.

    fp = fopen("asd.txt", "r") ; // using c standard library file open
    if (fp == NULL)
    {
        printf("Could not open file\n");
        return 0 ;
    }
    int itemsRead = fscanf(fp, "%s\n", input_line) ;
    //fscanf is a much more tractable reader. This will read one string of characters 
    // up to the end of line. It will easily and happily run past the end of input_line 
    // if the line is poorly formatted
    // handles a variety of EOL types. and returns the number of the requested 
    // items read. In this case, one item.
    printf("\n*******************************\n");
    printf("* Reading Program Into Memory *\n");
    printf("*******************************\n");
    while (itemsRead == 1 && input_line[0] != 'Z' && program_line < 100) 
    { // much better place for the exit conditions. Also added test to prevent 
      // overrunning memory
        for (int i = 0; i < 6 ; i++)
        {
            memory[program_line][i] = input_line[i] ;
        } // this can also be performed with memcpy
        printf("Program Line %d: ", program_line) ;
        for(int i = 0; i <  6; i++)
        {
            printf("%c", memory[program_line][i]) ;
        } // if we were using properly terminated c-style strings, and we are not, 
          // this loop and the following printf("\n") could be replaced with  
          // printf("%s\n", memory[program_line]). As it is putc would be a more 
          // efficient option
        printf("\n") ;

        opcode = (memory[program_line][0] -'0') *10 ;  // '0' much easier to read than 48
        opcode += memory[program_line][1] -'0' ;

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

        charRead = fscanf(fp, "%s\n", input_line) ;

        program_line++ ;
        printf("\n"); // fixed typo
    }
}
#包括
#包括
#包括
int main(int argc,char*argv[])
{
(void)argc;//我太迂腐了。正如下面评论中指出的,这
//不需要。Main不需要争论。我只是喜欢它们。
(无效)argv;
//将文件读入虚拟机
//将所有变量移到函数中
char memory[100][6];//如果读取超过100,则可能导致程序死亡
//文件中的行。没有任何防护措施可防止此情况发生
//在原始代码中。
int操作码;
int program_line=0;
FILE*fp;//为了最大的可移植性,使用C文件句柄而不是posix句柄
char input_line[8];//如果一行格式不正确并且有额外字符,
//但在这种情况下,整个程序都崩溃了。
//如果输入文件的格式为Windows,则使用8。
fp=fopen(“asd.txt”,“r”);//使用c标准库文件打开
如果(fp==NULL)
{
printf(“无法打开文件\n”);
返回0;
}
int itemsRead=fsc