简体   繁体   English

如何使用read()存储单词?

[英]How to store a word using read()?

About the program: Hello, I am writing a simple program to extract content from a .txt file and convert that content into a .csv file. 关于程序:您好,我正在编写一个简单的程序,以从.txt文件中提取内容并将该内容转换为.csv文件。 The plan is to look for specific words within that .txt file. 计划是在该.txt文件中查找特定的单词。 This is really just to experiment with the functions open(), read() , write() and close() in C on linux. 这实际上只是为了在Linux上的C语言中使用open(),read(),write()和close()函数进行实验。

The Problem: On line 34 of the code, I try to store each character coming in to form a word. 问题:在代码的第34行,我尝试存储每个输入的字符以形成一个单词。 After extracting a " " from the .txt, it will clear the word buffer. 从.txt中提取“”后,它将清除字缓冲区。 Problem is, I get a segmentation fault (core dump). 问题是,我遇到了分段错误(核心转储)。 I am not sure how to fix This problem. 我不确定如何解决此问题。 I tried using GDB to debug and find the seg fault at line 34. 我尝试使用GDB调试并在第34行找到seg错误。

Thank you in advance 先感谢您

The Code 编码

/* 
Program to convert content inside a .txt file 
into a .csv file.
*/

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>      // open()
#include <unistd.h>     // For read(), write() an close()
#include <string.h>     // Used for strcmp()

int main(int argc, char **argv){

    int samp = open("sample.txt", O_RDONLY);        // This is Opening a file to work with. @param char  *filename,  @param int  access,  @param int  permission
    int csv = open("sample.csv", O_WRONLY | O_CREAT, 0600);     // Used to create a file. 

    char *word;         // Stores each word 
    char buff[1];       // Holds 1 character of the file
    int i = 0;          // Counter for word buffer

    /* read(handle (file), buffer, size (bytes)) */
    /* write(handle (file), buffer, size (bytes)) */

    while(read(samp, buff, 1) != 0){    // Loops through file, char by char 
        printf("%s", buff);             // prints current character in buff

        if(strcmp(buff," ") == 0){      // To create csv, every " " found, we add a "," 
            write(csv, ",", 1);         // If " " is found, we write a comma to csv file
            word = "";                  // Clear word buffer
        }

        else{
            write(csv, buff, 1);        // Write value of buff in csv file
            word[i] = buff[0];              // Copy each characer in buff to word
        }

        i++;
    }

    close(samp);    // Closig .txt file
    close(csv);     // Closing .csv file

    return 0;
}

The problem is with 问题出在

 printf("%s", buff);

buff is not a string . buff不是字符串 You can either 你可以

  • define buff as two element array, char buff[2] = {0}; buff定义为两个元素数组, char buff[2] = {0}; and then use buff as a string . 然后使用buff作为字符串
  • define buff as a single char (not an array), pass &buff to read() call and use %c format specifier to print buff . buff定义为单个char (不是数组),将&buff传递给read()调用,并使用%c格式说明符打印buff
  • use %c and pass buff[0] . 使用%c并传递buff[0]

To elaborate, %s format specifier expects an argument as a pointer to a null-terminated char array. 详细说来, %s格式说明符期望将参数作为指向以null结尾的char数组的指针。 In your case, buff is one element too short to hold an input (from read() ) as well as the null-terminator. 在您的情况下, buff是一个太短的元素,无法容纳输入(来自read() )以及null终止符。 So, due to the property of %s , the out of bound access happens which invokes undefined behavior . 因此,由于%s的属性,会发生超出范围的访问,从而调用未定义的行为

I think that one of the problems you have is that you are writing word[i] = buff[0] , but word only ever points to a string constant, if anything at all ( "" , these are things you should not write to). 我认为,你有一个问题是,你写word[i] = buff[0]word永远只能指向一个字符串常量,如果在所有(任何东西""这些东西你应该写)。 You need to create a writable buffer to store the word. 您需要创建一个可写缓冲区来存储单词。

I also don't see you ever resetting i to 0 when you complete a word, so it will be forever trying to append to the same place. 我也看不到您在完成一个单词时将i重置为0 ,因此它将永远尝试附加到同一位置。

To address this, you could try changes like the following: 为了解决这个问题,您可以尝试进行如下更改:

char *word; -> char word[256]; /* NOTE: arbitrary max word size here, you will need to ensure that you don't overrun that */ -> char word[256]; /* NOTE: arbitrary max word size here, you will need to ensure that you don't overrun that */ char word[256]; /* NOTE: arbitrary max word size here, you will need to ensure that you don't overrun that */

word = ""; -> word[i] = '\\0'; i = 0; -> word[i] = '\\0'; i = 0; word[i] = '\\0'; i = 0; /* reset the string */ / *重置字符串* /

EDIT : Also, using strcmp to compare a single character is broken here as it is not a null terminated string. 编辑 :此外,使用strcmp比较单个字符在这里也被破坏了,因为它不是以null结尾的字符串。 Instead, just do something like if(buff[0] == ' ') 相反,只需执行类似if(buff[0] == ' ')

NOTE : I don't see you doing anything useful with this word buffer you are trying to assemble, you can probably just chop it entirely. 注意 :我看不到您尝试使用此word缓冲区来做任何有用的事情,您可以将其完全切碎。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 malloc、strtok_r 将输入作为字符串读取并存储每个单词(单词是由空格分隔的任何内容)到二维数组? - How do I read input as a string and store each word (a word is anything separated by a space) to a 2d array using malloc, strtok_r? 如何从非结构化的.txt文件中读取单词并将每个单词存储在C中的char数组中? - How to read words from unstructured .txt file and store each word in a char array in C? 如何在C语言中使用stdio.h逐字读取文件? - How can I read a file word by word using stdio.h in C? 如何使用read在数组中正确存储字符? - How do I properly store characters in an array using read? 如何使用 C 中的系统调用读取文件的每一行并将其存储在内存中 - How to read and store in memory each line of file using syscalls in C 从文件中读取行并将每个单词存储到数组(C语言)中 - read line from file and store each word into an array (C language) 如何使Trie存储单词在C中的出现 - How to make trie store reincidence of a word in C 如何将单词对从文件存储到字符串? - How to store word pairs from File to a String? 如何在文件中读取并存储它? - How do read in the file and store it? 如何将文件中的单词存储在单独的变量中? - How to store a word from file in a separate variable?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM