首页 > 解决方案 > 编译代码中无法解释的“没有这样的文件或目录”

问题描述

以下复制和修改的代码读取空格键以激活从连接的测试设备捕获数据。如果没有下面总结的干预代码,它运行良好。我通过创建 kbrd 命名空间并仅使用我需要的标准命令来避免“使用命名空间 std”。代码可以毫无困难地编译,但是当我使用必要的代码运行它以捕获键盘数据并初始化测试设备时,它会运行到“检查点 Alpha”并等待。当我按空格键(或任何其他键!)时,它会打印:

Check Point Beta
No such file or directory.

使用鼠标左键收集数据的非常相似的代码按预期运行。我正在构建一台小型计算机,使用“纸”显示器在户外便携式使用,并希望拥有不需要鼠标的代码......

我已经用完了试图解决这个问题的东西。请求您的帮助,我们将不胜感激。

    #include <stdlib.h>
    #include <unistd.h>
    #include <fcntl.h>
    #include <errno.h>
    #include <linux/input.h>
    #include <string.h>
    #include <stdio.h>
    #include <iostream>
    #include <fstream>
    #include <signal.h>
    #include <float.h>
    #include <sa_api.h>
    #pragma comment(lib,"sa_api.lib")
    #undef max
    #undef min

   namespace kbrd{   
     const char *dev = "/dev/input/event3";
     struct input_event ev;
     ssize_t n;
     int fd;}

   using std::cin;
   using std::cout;
   using std::__cxx11::string;
   using std::endl;
   using std::ofstream;
   using std::ios;

   int main(void)
   {
    [[Code which 
      Initiates equipment API, prompts screen & reads keyboard to 
      gather details to be stored with data from attached test 
      equipment...]]`

    while (N_az <Num_Samples) {

      kbrd::fd = open(kbrd::dev, O_RDONLY);
      if (kbrd::fd == -1) {
        fprintf(stderr, "Cannot open %s: %s.\n", kbrd::dev, 
        strerror(errno));
        return EXIT_FAILURE;
      }
      //and then read keyboard events from the device:
      cout << "Check Point Alpha" << endl;
      while (1) {
        kbrd::n = read(kbrd::fd, &kbrd::ev, sizeof kbrd::ev);
        if (kbrd::n == (ssize_t)-1) {
          if (errno == EINTR)
            continue;
          else
            break;
        } else
        if (kbrd::n != sizeof kbrd::ev) {
          errno = EIO;
          break;
        }
        cout << "Check Point Beta" << endl;

        if (kbrd::ev.type == EV_KEY && kbrd::ev.value == 0 /*&& 
            kbrd::ev.value <= 2 */&& kbrd::ev.code == 57){
          cout << "Check Point Charlie" << endl;            

          continue;

          [[ Code to read attached test gear & store data, one point at a 
             time...]]

          N_az = N_az + 1;
        }
        else {
          fflush(stdout);
          fprintf(stderr, "%s.\n", strerror(errno));
          return EXIT_FAILURE;
        }
      }
    } 

   return EXIT_SUCCESS;
 }

标签: g++

解决方案


当我按空格键(或任何其他键!)时,它会打印:

Check Point Beta
No such file or directory.

因为你的代码是

   cout << "Check Point Beta" << endl;

   if (kbrd::ev.type == EV_KEY && kbrd::ev.value == 0 /*&& 
       kbrd::ev.value <= 2 */&& kbrd::ev.code == 57){
     cout << "Check Point Charlie" << endl;            
     ...
   }
   else {
     fflush(stdout);
     fprintf(stderr, "%s.\n", strerror(errno));
     return EXIT_FAILURE;
   }

这意味着(kbrd::ev.type == EV_KEY && kbrd::ev.value == 0 /*&& kbrd::ev.value <= 2 */&& kbrd::ev.code == 57)是错误的,没有这样的文件或目录来自strerror所以你之前的某个地方有一个错误,可能不在read中,在read之前将errno重置为 0 。没有错误时的警告errno不会重置为0,它仅在发生错误时设置,因此您有责任将其重置为 0。

只需在测试前临时写入kbrd::ev.type / kbrd::ev.value / kbrd::ev.code的值并运行程序即可知道按下空格键时的值是什么,您可以修改您的结果代码


推荐阅读