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

huangapple go评论126阅读模式
英文:

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 &lt;stdio.h&gt; 
#include &lt;ncurses.h&gt;
#include &lt;stdlib.h&gt;  //relates to string.h
#include &lt;string.h&gt;  // 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 &quot;run&quot; as a variable, this will be used later in the program to tell the program to move on
  logo = fopen(&quot;assets/log.ass&quot;, &quot;r&quot;);  // Opens the logo file and puts it into ram as &quot;log&quot;
  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 &quot;printw&quot; instead of &quot;printf&quot; to display text
  do { //prints logo to screen 
  line = fgetc(logo); // Saves first line of log in pre-allocated memory
  printw(&quot;%c&quot;, line); // Prints the var to the screen
} while(logo != EOF);

  getch();  //Waits for user to press any key but soon I will make it &quot;Enter&quot; key specific&quot;

  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&#39; in read-only section `.text&#39;
/usr/bin/ld: /tmp/ccYN1f5L.o: in function `main&#39;:
Main.c:(.text+0x2a): undefined reference to `initscr&#39;
/usr/bin/ld: Main.c:(.text+0x2f): undefined reference to `raw&#39;
/usr/bin/ld: Main.c:(.text+0x58): undefined reference to `printw&#39;
/usr/bin/ld: Main.c:(.text+0x66): undefined reference to `stdscr&#39;
/usr/bin/ld: Main.c:(.text+0x6e): undefined reference to `wgetch&#39;
/usr/bin/ld: Main.c:(.text+0x73): undefined reference to `endwin&#39;
/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 &lt;ncurses.h&gt;
#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;string.h&gt;

int main() {
	FILE *logo = fopen(&quot;assets/log.ass&quot;, &quot;r&quot;);
	if(!logo) {
        printf(&quot;failed to open file\n&quot;);
		return EXIT_FAILURE;
    }
	initscr();
	raw();
	for(;;) {
		int ch = fgetc(logo);
		if(ch == EOF)
			break;
		printw(&quot;%c&quot;, ch);
	}
	getch();
	endwin();
}

huangapple
  • 本文由 发表于 2023年3月4日 09:23:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/75633111.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定