我和小伙伴在做一个简单的项目,这次我们决定用C。我们对 C 还比较陌生,但无论如何都想试一试。我们正在尝试读取文件并将其打印到屏幕上。
这是我们的代码:
//imports libraies
#include <stdio.h>
#include <ncurses.h>
#include <stdlib.h> //relates to string.h
#include <string.h> // adds support for string type variables
// Marks anything between the the next curly brackest as the main function
int main() {
//declaring vars
FILE* logo; // Makes logo a pointer
int run; // sets "run" as a variable, this will be used later in the program to tell the program to move on
logo = fopen("assets/log.ass", "r"); // Opens the logo file and puts it into ram as "log"
char line; //Makes variable that will store each line of file read from
//setting up ncurses
initscr(); //Creates stdscr *Used by ncurses for keyboard input*
raw(); //defines ncurses mode, raw should be used for now
//Displays opening screen and prompts the user to press enter to start the program
//While using ncurses like we are now use "printw" instead of "printf" to display text
do { //prints logo to screen
line = fgetc(logo); // Saves first line of log in pre-allocated memory
printw("%c", line); // Prints the var to the screen
} while(logo != EOF);
getch(); //Waits for user to press any key but soon I will make it "Enter" key specific"
endwin(); //kills ncurses. If this command is not called the program will not close right, I will put it above return for now
return 0;
}
当我尝试编译和链接我的代码时,编译器会这样说:
Main.c: In function ‘main’:
Main.c:24:14: warning: comparison between pointer and integer
24 | } while(logo != EOF);
| ^~
/usr/bin/ld: /tmp/ccYN1f5L.o: warning: relocation against `stdscr' in read-only section `.text'
/usr/bin/ld: /tmp/ccYN1f5L.o: in function `main':
Main.c:(.text+0x2a): undefined reference to `initscr'
/usr/bin/ld: Main.c:(.text+0x2f): undefined reference to `raw'
/usr/bin/ld: Main.c:(.text+0x58): undefined reference to `printw'
/usr/bin/ld: Main.c:(.text+0x66): undefined reference to `stdscr'
/usr/bin/ld: Main.c:(.text+0x6e): undefined reference to `wgetch'
/usr/bin/ld: Main.c:(.text+0x73): undefined reference to `endwin'
/usr/bin/ld: warning: creating DT_TEXTREL in a PIE
collect2: error: ld returned 1 exit status
我已经修复了许多语法错误,这很有帮助,但我似乎无法解决这个问题。
您的代码依赖于您未提供的文件 assets/log.ass,因此无法对其进行测试,但这会在没有警告的情况下编译
gcc 1.c -lcurses
:
#include <ncurses.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main() {
FILE *logo = fopen("assets/log.ass", "r");
if(!logo) {
printf("failed to open file\n");
return EXIT_FAILURE;
}
initscr();
raw();
for(;;) {
int ch = fgetc(logo);
if(ch == EOF)
break;
printw("%c", ch);
}
getch();
endwin();
}