英文:
I'm trying to read from a file in C and I have debugged all my errors except for one that I can't figure out
问题
这是你的代码翻译:
我的朋友和我正在开展一个简单的项目,我们决定这次使用C语言。我们对C语言相对不太熟悉,但还是想试试看。我们试图读取一个文件并将其打印到屏幕上。
这是我们的代码:
// 导入库
#include <stdio.h>
#include <ncurses.h>
#include <stdlib.h> // 与string.h有关
#include <string.h> // 添加对字符串类型变量的支持
// 将下一个花括号之间的任何内容标记为主函数
int main() {
// 声明变量
FILE* logo; // 使logo成为一个指针
int run; // 将“run”设置为一个变量,稍后在程序中将用于指示程序继续执行
logo = fopen("assets/log.ass", "r"); // 打开logo文件并将其存储在内存中作为“log”
char line; // 创建一个变量,用于存储从文件中读取的每一行
// 设置ncurses
initscr(); // 创建stdscr *用于ncurses的键盘输入*
raw(); // 定义ncurses模式,目前应该使用“raw”
// 显示开场画面并提示用户按Enter键启动程序
// 当使用像现在这样的ncurses时,使用“printw”而不是“printf”来显示文本
do { // 将logo打印到屏幕上
line = fgetc(logo); // 将log的第一行保存在预分配的内存中
printw("%c", line); // 将变量打印到屏幕上
} while (line != EOF);
getch(); // 等待用户按任意键,但很快我将使其特定于“Enter”键
endwin(); // 关闭ncurses。如果不调用此命令,程序将无法正确关闭,我现在将其放在return之前
return 0;
}
当我尝试编译和链接我的代码时,编译器报告了以下错误:
Main.c: 在函数‘main’中:
Main.c:24:14: 警告:指针和整数之间的比较
24 | } while (line != EOF);
| ^~
/usr/bin/ld: /tmp/ccYN1f5L.o: 警告:在只读段“.text”中针对‘stdscr'的重定位
/usr/bin/ld: /tmp/ccYN1f5L.o: 在函数‘main’中:
Main.c:(.text+0x2a): 对‘initscr'的未定义引用
/usr/bin/ld: Main.c:(.text+0x2f): 对‘raw'的未定义引用
/usr/bin/ld: Main.c:(.text+0x58): 对‘printw'的未定义引用
/usr/bin/ld: Main.c:(.text+0x66): 对‘stdscr'的未定义引用
/usr/bin/ld: Main.c:(.text+0x6e): 对‘wgetch'的未定义引用
/usr/bin/ld: Main.c:(.text+0x73): 对‘endwin'的未定义引用
/usr/bin/ld: 警告:在PIE中创建DT_TEXTREL
collect2: 错误:ld返回1退出状态
我已经修复了很多语法错误,但似乎无法解决这个问题。
英文:
My buddy and I are working on a simple project, and we decided to go with C this time. We are relatively new to C but wanted to give it a shot anyways. We are trying to read a file and print it to the screen.
Here is our code:
//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;
}
When I try to compile and link my code, the compiler says this:
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
I have fixed many syntax errors which has helped, but I can't seem to figure this problem out.
答案1
得分: 1
您的代码依赖于未提供的文件"assets/log.ass"。这意味着我无法测试它,但使用gcc 1.c -lcurses
编译时没有警告。它检查了fopen()
的返回值,并更重要的是将fgetc()
的返回值类型更改为int
,在使用之前检查是否为EOF:
#include <ncurses.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main() {
FILE *logo = fopen("assets/log.ass", "r");
if(!logo) {
printf("无法打开文件\n");
return EXIT_FAILURE;
}
initscr();
raw();
for(;;) {
int ch = fgetc(logo);
if(ch == EOF)
break;
printw("%c", ch);
}
getch();
endwin();
}
英文:
Your code relies on the file assets/log.ass which you did not supply. This means I cannot test it but this compiles without warning with gcc 1.c -lcurses
. It checks the return value for fopen()
, and more importantly changes the type of the variable for the return value of fgetc()
to an int
and checks it for EOF before use:
#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();
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论