将read()读取的数据写入C中的int数组 [英] Writing data read with read () to int array in C
问题描述
我有一个文件,每行都有数字.我正在尝试使用 read() 函数将从文件中读取的数据写入 int 数组.我可以读取文件并将其打印到终端.如何获取读入 arr 数组的数字?
I have a file with numbers on each line. I am trying to write the data read from the file to the int array with the read () function. I can read the file and print it to the terminal. How can i get the numbers i read into arr array ?
这是我的代码
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
int main() {
int len, fd,arr[10000],i=0;
fd = open("1.txt", O_RDONLY);
if (fd >= 0) {
while(read(fd, &len, sizeof(int)) > 0){
write(1, &len, sizeof(int));
}
}
close(fd);
return 0;
}
推荐答案
在大多数(所有?)实现中,整数将数字的二进制表示存储在该整数的位和字节中.另一方面,字符串和诸如文本文件之类的东西使用字节来存储数字、空格和换行符的 ASCII 值.
On most (all?) implementations, a integer stores the binary representation of a number in the bits and bytes of that integer. In the other hand, a string and something like your text file uses bytes to store the ASCII value of digits, spaces and line feeds.
int
(假设 4 个字节,每个字节 8 位,大端)可以这样存储值 1234:
int
(assuming 4 byte with 8 bit each, big endian) may store the value 1234 this way:
Address 0x42 0x43 0x44 0x45
Value 0x00 0x00 0x04 0xD2
Text NUL NUL EOT Ò
另一方面,字符串可以包含代表文本的每个字符的 ASCII 值.字符串1234"可以这样存储:
In the other hand, a string can contain the ASCII values of each character that represents a text. The String "1234" could be stored like this:
Address 0x82 0x83 0x84 0x85 0x86
Value 0x31 0x32 0x33 0x34 0x00
Text 1 2 3 4 NUL
读取时,您读取文本文件的字符.将它们读入 char
数组很容易,您不需要进行任何对话,只需在末尾添加 NUL 字节即可.当你想得到数字时,你必须将它们从字符串中转换.
When you do a read, you read the characters of the text file. Reading them into a char
array is easy, you do not need to do any conversation, only add the NUL-Byte at the end. When you want to get the number, you have to convert them from a string.
这意味着您必须读取文件,如果需要,您可以使用 read()
执行此操作,并将内容存储在 char
数组中,添加一个 NUL-Byte 然后使用类似 strtol()
或 sscanf()
的函数转换结果字符串.
This means you have to read the file, you can do this with read()
if you want, and store the content in a char
array, add a NUL-Byte and then convert the resulting string with a function like strtol()
or sscanf()
.
您所做的是将 ASCII 字符读入 int
len
.当您在 write()
调用之前使用调试器时,您可以检查 len
的值.就我而言,我将其用作输入文件:
What you do is reading the ASCII characters into the int
len
. When you use a debugger before the write()
call, you can check the value of len
. In my case i used this as an input file:
0
1
2
3
...
当我在 write()
之前停止调试器时,我看到 len
的值为 170986032 == 0xA310A30
.我的系统是小端的,这意味着最低字节存储在最低地址(与我之前的示例不同).这意味着 0x30
首先出现,然后是 0x0a
、0x31
和 0x0A
.由此我们知道我们得到了如下len
的内存布局.
When i stop my debugger before write()
, i see that len
has the value of 170986032 == 0xA310A30
. My system is little endian, means the lowest byte is stored at the lowest address (unlike my previous example). Which means 0x30
comes first, then 0x0a
, 0x31
and 0x0A
.
From this we know that we got the following memory layout of len
.
Address Offset 0x00 0x01 0x02 0x03
Value 0x30 0x0A 0x31 0x0A
Text 0 LF 1 LF
如您所见,文本被解释为 int
.
As you can see, the text is interpreted as a int
.
您想将数据存储到 char
数组中.然后解析它.我使用一些伪代码来更好地解释它.这不是 C 代码.您应该学习编写自己的代码.这只是为了让您了解您必须做什么:
You want to store the data into a char
array. And then parse it. I use some pseudo code to explain it better. This is not C-Code. You should learn to write your own code. This is just to get you an idea what you have to do:
char buffer[<buffersize>] //create a buffer array
r=read(buffer) //read the file into the buffer. You may need to repeat that multiple times to get everything
if r==error //check for errors
<do some error handling here>
buffer[<one after the last read byte>]='\0' //add the NUL-Byte st the end, so that we have a string.
int values[<number of ints you want>] //create an array to store the parsed values
for(<one loop for every int>) //make the loop for every int, to parse the int
values[<index>]=strtol(buffer,&buffer,0) //parse the text to a int
if error occured:
<do some error handling here>
如何在 C 中实现它是您的任务.请记住缓冲区大小,以免以 UB 结尾.
How to implement this in C is your task. Keep buffer sizes in mind so you do not end with UB.
这篇关于将read()读取的数据写入C中的int数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!