Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在C语言中,如何从文件中获取一些值并将其存储到数组中_C - Fatal编程技术网

在C语言中,如何从文件中获取一些值并将其存储到数组中

在C语言中,如何从文件中获取一些值并将其存储到数组中,c,C,我正在尝试使用open/read系统调用从输入文件中获取一些值,如下所示: #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <fcntl.h> #include <stddef.h> #include <semaphore.h> #include <sys/stat.h>

我正在尝试使用open/read系统调用从输入文件中获取一些值,如下所示:

    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <fcntl.h>
    #include <stddef.h>
    #include <semaphore.h>
    #include <sys/stat.h>
    #include <sys/types.h>
    #include <sys/wait.h>
    #include <errno.h>

        #define BUFFER_SIZE 1024

        int main (int argc, const char * argv[] ){

        int inputFile;
        char buffer[BUFFER_SIZE];

        int heed;

        if (argc < 1){
            perror("There isn't any file");
            return -1;
        }
        else {
        input = open(argv[1], O_RDONLY);
        if (inputFile < 0){
            printf("Error, execution interrupted\n");
            return -1;
        }

        inputFile = open(datos,O_RDWR);
        while((heed = read(inputFile,buffer,BUFFER_SIZE)) != 0);
    printf("The first value stored in buffer is: %s\n",buffer[0]);
}
 return 0
 }
输入文件包含以下值,用空格分隔:

4 5 5 2 1 2 3 3 5 2 
您可以这样使用:

#include <stdlib.h>
#include <stdio.h>
#include <errno.h>

#define BUFFER_SIZE 1024

int main(int argc, char *argv[]) {
  char buffer[BUFFER_SIZE];
  if(argc < 2) {
    fprintf(stderr, "Please provide file name\n");
    return EXIT_FAILURE;
  }
  FILE *pFile = fopen(argv[1], "r+");
  if(pFile == NULL) {
    fprintf(stderr, "No such file\n");
    return EXIT_FAILURE;
  }

  int nBufferLen = 0;
  while(nBufferLen < BUFFER_SIZE) {
    int nRet = fscanf(pFile, "%c", &buffer[nBufferLen]);
    if(nRet == EOF) break;
    else if(nRet == 1) { /* read successfully */ }
    else {
      printf("No Match\n");
    }
    nBufferLen++;
  }
  int i;
  for(i = 0; i < nBufferLen; i++)
    printf("%c ", buffer[i]);
  printf("\n");
  fclose(pFile);
  return 0;
}

我希望这能有所帮助。

请编辑您的问题,使其包含可编译代码。还请编辑它以显示打印值的位置-我在问题中看不到-是的,您有一个名为read的局部变量,它隐藏了函数read。请注意您的编译器或准确发布您正在编译的代码。问题已编辑。
如果(inputFile<0)
--
inputFile
未初始化,
%s\n”,缓冲区[0]
→ <代码>%s\n“,缓冲区
如果(argc<1){
→ <代码>如果(argc<2){
(第一个参数是程序的名称)非常感谢,这是非常有用的。
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>

#define BUFFER_SIZE 1024

int main(int argc, char *argv[]) {
  char buffer[BUFFER_SIZE];
  if(argc < 2) {
    fprintf(stderr, "Please provide file name\n");
    return EXIT_FAILURE;
  }
  FILE *pFile = fopen(argv[1], "r+");
  if(pFile == NULL) {
    fprintf(stderr, "No such file\n");
    return EXIT_FAILURE;
  }

  int nBufferLen = 0;
  while(nBufferLen < BUFFER_SIZE) {
    int nRet = fscanf(pFile, "%c", &buffer[nBufferLen]);
    if(nRet == EOF) break;
    else if(nRet == 1) { /* read successfully */ }
    else {
      printf("No Match\n");
    }
    nBufferLen++;
  }
  int i;
  for(i = 0; i < nBufferLen; i++)
    printf("%c ", buffer[i]);
  printf("\n");
  fclose(pFile);
  return 0;
}
~/Documents/src : $ gcc arrayFile.c 
~/Documents/src : $ ./a.out stuff.txt 
4   5   5   2   1   2   3   3   5   2