[英]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 你可以
buff
as two element array, char buff[2] = {0};
将buff
定义为两个元素数组, char buff[2] = {0};
and then use buff
as a string . 然后使用buff
作为字符串 。 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
。 %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.