C 如何从read()中获取字符

C 如何从read()中获取字符,c,C,我正在尝试复制一个文件,以便仅使用open()、read()和write()将其向后打印。 然而,我想知道是否有一种方法可以从读取中获取返回读取字节数的char指针。如果是这样的话,我该怎么做呢?我试过一次,但最后还是出了错 error: invalid type argument of ‘unary *’ (have ‘ssize_t’) 这就是我使用的代码片段 #include <stdio.h> #include <unistd.h> #include <s

我正在尝试复制一个文件,以便仅使用
open()
read()
write()
将其向后打印。 然而,我想知道是否有一种方法可以从读取中获取返回读取字节数的char指针。如果是这样的话,我该怎么做呢?我试过一次,但最后还是出了错

error: invalid type argument of ‘unary *’ (have ‘ssize_t’)
这就是我使用的代码片段

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

int main (int argc, char *argv[])
{
 if(argc != 3)/*argc should be 2 for correct execution*/
 {
  printf("usage: %s filename",argv[0]);
 }
 else
 {
  int file1 = open(argv[1], O_RDWR);
  if(file1 == -1){
    printf("\nfailed to open file.");
    return 1;
  }
  int reversefile = open(argv[2], O_WRONLY, O_CREAT);
  int size =(int)lseek(file1, 0, SEEK_END)+1;
  char file2[size];
  int count=size;
  int i = 0;

  while(read(file1, &file2[count], 1) != 0)
  {
   *file2[i]=*read(file1, file[count], 1);
   write(reversefile, file2[i], size+1);
   count--;
   i++;
   lseek(reversefile, i, SEEK_SET);
   }
}
#包括
#包括
#包括
#包括
#包括
int main(int argc,char*argv[])
{
如果(argc!=3)/*argc应为2以正确执行*/
{
printf(“用法:%s文件名”,argv[0]);
}
其他的
{
int file1=打开(argv[1],O_RDWR);
如果(文件1==-1){
printf(“\n无法打开文件”);
返回1;
}
int reversefile=open(argv[2],O_WRONLY,O_CREAT);
int size=(int)lseek(file1,0,SEEK_END)+1;
char file2[size];
int计数=大小;
int i=0;
while(读取(file1和file2[count],1)!=0)
{
*file2[i]=*读取(file1,file[count],1);
写入(反转文件,文件2[i],大小+1);
计数--;
i++;
lseek(反转文件,i,SEEK_集);
}
}

read
返回指示成功读取多少字节的
size\t
值,但不返回指针

出于您的目的,首先需要使用调用
read

read(file1, &file2[count], 1)
然后,您可以通过以下方式访问从文件读取的值:

file2[i] = file[count];
而且我认为你不需要打两次电话给read

此外,如果您只需要一个字符,那么使用
getc
更好

char c;
while((c=getc(file1)) != EOF){
  // use c
}  
  • 您需要从文件中读取所有行,并将它们保存到字符串数组中。 这是一个由100个字符串组成的数组,每个字符串最多可以包含999个字符+1(“\0”)

  • 你需要反转字符串。这可以通过非常简单的算法来完成。例如:如果你的字符串是“ABCDE”,你可以交换“A”和“E”、“B”和“D”。每次你把你的For-loot换成strlen(myfile[i])/2,然后交换第i位和第(strlen(myfile[i])-1-i-th位的字符

  • 现在您可以在output.txt文件中写入所有字符串,但顺序相反

    #include <stdio.h>
    #include <string.h>
    
    char myfile[100][1000];
    int n;
    
    void readmyfile(void)
    {
        int i;
        FILE *input = fopen("input.txt", "r");
        printf("This is in your file:\n");
        for(i = 0; fscanf(input, "%[^\n] ", myfile[i]) != EOF; i++)
            printf("%s\n", myfile[i]);
        n = i;
        fclose(input);
    }
    
    void reverseall(void)
    {
        int i, j, len;
        char swap;
        for(i = 0; i < n; i++)
        {
            len = strlen(myfile[i]);
            for(j = 0; j < len / 2; j++)
            {
                swap = myfile[i][j];
                myfile[i][j] = myfile[i][len - 1 - j];
                myfile[i][len - 1 - j] = swap;
            }
        }
    }
    
    void writetooutput(void)
    {
        int i;
        FILE *output = fopen("output.txt", "w");
        printf("This is your output file:\n");
        for(i = n - 1; i >= 0; i--)
        {
            fprintf(output, "%s\n", myfile[i]);
            printf("%s\n", myfile[i]);
        }
        fclose(output);
    }
    
    int main()
    {
        readmyfile();
        reverseall();
        writetooutput();
        return 0;
    }
    
    #包括
    #包括
    char myfile[100][1000];
    int n;
    void readmyfile(void)
    {
    int i;
    FILE*input=fopen(“input.txt”,“r”);
    printf(“这在您的文件中:\n”);
    对于(i=0;fscanf(输入“%[^\n]”,myfile[i])!=EOF;i++)
    printf(“%s\n”,myfile[i]);
    n=i;
    fclose(输入);
    }
    作废撤销所有(作废)
    {
    int i,j,len;
    字符交换;
    对于(i=0;i=0;i--)
    {
    fprintf(输出,“%s\n”,我的文件[i]);
    printf(“%s\n”,myfile[i]);
    }
    fclose(输出);
    }
    int main()
    {
    readmyfile();
    reverseall();
    writetooutput();
    返回0;
    }
    

  • 反向?为了好玩,一个递归解决方案。不需要
    stat()
    seek()

    注意
    ssize\u t len=read(inf,buf,N);
    len
    ,如果>=0是读取的字节数,则会发生错误。
    len
    因为0通常表示文件结束

    #include <stdlib.h>
    #include <stdio.h>
    #include <unistd.h>
    
    int read_R(int inf, size_t N) {  // return 0 is good, else error
      int result = 0;
      char *buf = malloc(N);
      if (buf == NULL) {
        return -1;  // Out of memory
      }
      ssize_t len = read(inf, buf, N);
      if (len > 0) {
        // read & print remainder of file
        result = read_R(inf, N*2);
        // print this buffer in reverse
        while (len > 0) {
          putchar(buf[--len]);
        }
      }
      free(buf);
      if (len < 0) { // Some I/O error, see errno
        return 1;
      }
      return result;
    }
    
    // Usage
    int inf = open(...);
    if (read_R(inf, 4096)) {
      ; // error
    }
    else {
      ; // OK, every thing read and printed in reverse, memory free'd
    }
    close(inf); 
    
    #包括
    #包括
    #包括
    int read_R(int inf,size_t N){//返回0良好,否则错误
    int结果=0;
    char*buf=malloc(N);
    如果(buf==NULL){
    return-1;//内存不足
    }
    ssize_t len=读取(inf,buf,N);
    如果(len>0){
    //读取并打印文件的其余部分
    结果=读取(inf,N*2);
    //反向打印此缓冲区
    而(len>0){
    putchar(buf[--len]);
    }
    }
    免费(buf);
    如果(len<0){//出现一些I/O错误,请参阅errno
    返回1;
    }
    返回结果;
    }
    //用法
    int inf=打开(…);
    如果(读(inf,4096)){
    ;//错误
    }
    否则{
    ;//好的,所有的东西都是反向读取和打印的,没有内存
    }
    关闭(inf);
    
    谢谢你的回答。但是我认为文件2[i]=文件计数将使我获得所需的字符。我上载了所有代码以使其更清晰。我希望遍历该文件并逐字符读取它,然后在另一个文件中向后重新创建它。我读取了第二次读取,这在我读完后是有意义的,但我仍然不确定如何获取字符并将其分配给数组。@user2872131已编辑,您是否错过了帖子中的
    &
    ?否则它将无法为我编译。我添加了
    &
    ,但它仍然无法编译,我得到了相同的错误。
    &
    有什么作用呢?顺便问一下,还有没有其他方法将字符放入数组中,因为我同意我可能不需要再次读取,但我会如何获得字符?创建一个读写文件。我键入的错误吗?您是否需要读取输出文件?不需要。我将把它改为wronly
    #include <stdlib.h>
    #include <stdio.h>
    #include <unistd.h>
    
    int read_R(int inf, size_t N) {  // return 0 is good, else error
      int result = 0;
      char *buf = malloc(N);
      if (buf == NULL) {
        return -1;  // Out of memory
      }
      ssize_t len = read(inf, buf, N);
      if (len > 0) {
        // read & print remainder of file
        result = read_R(inf, N*2);
        // print this buffer in reverse
        while (len > 0) {
          putchar(buf[--len]);
        }
      }
      free(buf);
      if (len < 0) { // Some I/O error, see errno
        return 1;
      }
      return result;
    }
    
    // Usage
    int inf = open(...);
    if (read_R(inf, 4096)) {
      ; // error
    }
    else {
      ; // OK, every thing read and printed in reverse, memory free'd
    }
    close(inf);