编译接收文件的c++程序的问题

Issues with compiling a C++ program taking in a file

本文关键字:c++ 问题 程序 文件 编译      更新时间:2023-10-16

由于某种原因,我在编译时遇到了一些问题,我正在试图找出发生这种情况的确切原因:

g++ Assignment1.cpp test.txt
 warning: format not a string literal and no format arguments [-Wformat-security]
         printf(op); // To test whether what's scan is what needs to be scanned.
                  ^
/usr/bin/ld:test.txt: file format not recognized; treating as linker script
/usr/bin/ld:test.txt:1: syntax error
collect2: error: ld returned 1 exit status

程序本身:

string op;
    char output;
    int i,trig[6];
    FILE *file_ptr;
    file_ptr = fopen(argv[1],"r+");
    if (file_ptr != NULL){
        fscanf(file_ptr,"%1s (%d,%d) (%d,%d) (%d,%d)", &output, &trig[1], &trig[2],&trig[3],&trig[4],&trig[5],&trig[6]);
        char *op = &output;
        printf(op); // To test whether what's scan is what needs to be scanned.
    }
    else {
        printf("File Not Found!");
    }
    fclose(file_ptr);
return 0;
}

和test.txt文件是简单的:T(30,20) (34,30) (12,25)

您也可以这样打开文件我已经删除了字符串op,因为你已经声明了相同名称的char *op

   file_ptr=fopen("test.txt","r");

//你的代码现在应该看起来像这样

  int main(){
//string op;
char output;
int i,trig[6];
FILE *file_ptr;
file_ptr = fopen("test.txt","r");
if (file_ptr != NULL){
    fscanf(file_ptr,"%1s (%d,%d) (%d,%d) (%d,%d)", &output, &trig[1], &trig[2],&trig[3],&trig[4],&trig[5],&trig[6]);
    char *op = &output;
    printf(op); // To test whether what's scan is what needs to be scanned.
}
else {
    printf("File Not Found!");
}
fclose(file_ptr);
 return 0;
 }