[英]C system calls — keep getting error with lseek and read
This is a practice exercise I am working on for class and I don't understand why this won't run... 这是我正在上课的练习练习,我不明白为什么它不会运行...
I get the problem when trying to assign a char array (buffer) with a length from a variable (num2). 尝试从变量(num2)分配长度的char数组(缓冲区)时遇到问题。
You can execute the file like so: 您可以像这样执行文件:
./file.c offset numOfChars filename.txt
./file.c 4 10 somefile.txt
If somefile contained the text: 如果somefile包含文本:
Why isn't this c program working.
为什么这个C程序不起作用。 I can't figure it out
我不知道
The program should print 该程序应打印
isn't this
不是吗
Here is the code: 这是代码:
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
main(int ac, char *av[]){
// Save the command line variables
int num1 = av[1];
int num2 = av[2];
long numbyte1 = av[1];
long numbyte2 = av[2];
int fd = open(av[3], O_RDONLY);
// Try to open the file
if( fd < 0 )
perror(fd + " - Could not open file!");
// use stat to get file size
struct stat sb;
if(fstat(fd,&sb) < 0)
return 1;
// Check to see if the file is big enough for the provided offset
if(sb.st_size < num1+num2){
perror(fd + " - Size of file is not large enough for provided offset!" + fd);
}
char buffer[num2];
if(lseek(fd, numbyte1 ,SEEK_SET) < 0) return 1;
if(read(fd, buffer, numbyte2) != numbyte2) return 1;
printf("%s\n", buffer);
return 0;
}
Issues that I see: 我看到的问题:
./file.c
is not the proper way to run the program. ./file.c
不是运行程序的正确方法。 You need to compile the program and create an executable. 您需要编译程序并创建可执行文件。 Then, you can run it.
然后,您可以运行它。
If you have gcc
, use: 如果您有
gcc
,请使用:
gcc -o file -Wall file.c ./file 4 10 somefile.txt
These lines 这些线
int num1 = av[1]; int num2 = av[2];
are not right. 不对。 The compiler should report warnings.
编译器应报告警告。 Using
gcc
, I get the following warnings for those two lines: 使用
gcc
,对于这两行我得到以下警告:
\nsoc.c: In function 'main':soc.c:在函数“ main”中:\nsoc.c:4:15: warning: initialization makes integer from pointer without a cast [enabled by default]
soc.c:4:15:警告:初始化从指针生成整数而不进行强制转换[默认启用]\nint num1 = av[1];
int num1 = av [1];\n ^
^\nsoc.c:5:15: warning: initialization makes integer from pointer without a cast [enabled by default]
soc.c:5:15:警告:初始化从指针生成整数而不进行强制转换[默认启用]\nint num2 = av[2];
int num2 = av [2];\n
av[1]
and av[2]
are of type char*
. av[1]
和av[2]
的类型为char*
。 If the contain integers, you can extract the integers from them by using one of several functions from the standard library. 如果包含整数,则可以使用标准库中的多个函数之一从整数中提取整数。 Eg
例如
int num1 = atoi(av[1]); int num2 = atoi(av[2]);
The lines 线
long numbyte1 = av[1]; long numbyte2 = av[2];
suffer from the same problem. 遭受同样的问题。 You can use the already extracted numbers to intiaize
numbypte1
and numbypte2
您可以使用已提取的数字来初始化
numbypte1
和numbypte2
long numbyte1 = num1; long numbyte2 = num2;
You have 你有
char buffer[num2];
that will be not enough to hold a string that has num2
characters. 不足以容纳具有
num2
字符的字符串。 You need another element in the array to hold the terminating null character. 您需要数组中的另一个元素来保存终止空字符。 Use:
采用:
char buffer[num2+1];
Add a terminating null character to buffer
after you read the data from the file. 从文件中读取数据后,将一个终止的空字符添加到
buffer
。
if(read(fd, buffer, numbyte2) != numbyte2) return 1; buffer[num2] = '\\0';
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.