我正在创建一个函数来将一个字符数组复制到C中的文件中。我收到有关指针的警告,所以我很确定这个问题。在我的主程序中,我进行了系统调用:
int main(int argc, char *argv[])
{
int *out_fd = open("test.txt", O_RDWR, 0644); //Yes, I will fix the 3rd argument
char arr[] = "hello";
copyCharArr(arr, out_fd);
}
copyCharArr()函数在这里:
int copyCharArr(char *arr[], int fd)
{
int num_written = write(fd, arr, sizeof(arr));
if(num_written == -1){
printf("Unable to write file\n");
return -1;
}
if(num_written != sizeof(arr)){
printf("Unable to write entire file\n");
return -1;
}
return 0;
}
以下是警告:
options.c: In function ‘addFile’:
options.c:22: warning: initialization makes pointer from integer without a cast
options.c:24: warning: passing argument 1 of ‘copyCharArr’ from incompatible pointer type
copies.h:5: note: expected ‘char **’ but argument is of type ‘char *’
任何帮助都表示赞赏,因为我对指针有点生疏!我甚至不确定是否可以将文件传递给这样的函数。
答案 0 :(得分:1)
你有几个错误:
int copyCharArr(char *arr[], int fd)
// ^^^^^^
// Use char arr[]
// or
// char* arr
{
// sizeof(arr) does not give you the length of the string.
// it gives you the size of a pointer. You need strlen(arr)
// write(fd, arr, strlen(arr));
int num_written = write(fd, arr, sizeof(arr));
if(num_written == -1){
printf("Unable to write file\n");
return -1;
}
// Once again, sizeof(arr) is not appropriate.
// Use strlen(arr)
if(num_written != sizeof(arr)){
printf("Unable to write entire file\n");
return -1;
}
return 0;
}
更新版本。
int copyCharArr(char *arr, int fd)
{
int len = strlen(arr);
int num_written = write(fd, arr, len);
if(num_written == -1){
printf("Unable to write file\n");
return -1;
}
if(num_written != len){
printf("Unable to write entire file\n");
return -1;
}
return 0;
}
答案 1 :(得分:1)
下面的代码中修复了一些错误和不准确之处。请参阅有关详细信息的评论:
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
int copyCharArr(char *, const size_t, int); /* A prototype is mandatory as the
compiler had not seen copyCharArr() yet. */
/* int main(int argc, char *argv[]) */
int main(void) /* If argc and argv aren't used to not define them. */
{
int out_fd = open("test.txt", O_RDWR, 0644); /* open() returns int so declare
out_fd as such. */
char arr[] = "hello";
copyCharArr(arr, sizeof arr, out_fd); /* As the size of arr gets lost when passing
it to a function pass it explicitly.
Pass sizeof arr - 1 if the "string"'s
0-terminator shall not be written to file.*/
/* Do not forget to close the file. */
}
int copyCharArr(char * arr, const size_t s, int fd) /* Passing a char[] to a function
let's it decay to a pointer to its
1st element, that is char* here.
That's also why we need to pass the
size explcitly as s. */
{
ssize_t result = write(fd, arr, s); /* write() returns ssize_t not int. */
if (result == -1) {
perror("write() failed"); /* Using this library function gives more details on
failure. Also the error goes to stderr, where error
messages shall go. */
return -1;
}
{
size_t num_written = result;
if (num_written != s) {
fprintf(stderr, "Unable to write entire file.\n"); /* Prints the error message to
stderr, but to standard output. */
return -1;
}
}
return 0;
}
答案 2 :(得分:0)
open返回一个低级整数文件描述符。使用int指针无效
int fd = open(...) // correct
不
int *fd = open(...) // error