读取一行并从文件的矩阵中删除空格

时间:2019-05-07 19:36:35

标签: c

好的,所以我设法用char写到文件char,但是它写了两倍的数字,并且仍然用空格写。有什么建议和解决方案吗?

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/types.h>
#include <unistd.h>
#include <ctype.h>
#include <fcntl.h>

extern int errno;
#define MAX_LEN 18

int main(int argc, char *argv[]) {
    int fd[2], des, bytes, target;
    char buffer[161];
    int fdr, fdw;   // file descriptors
    char c;

    fdr = open(argv[1],  O_RDONLY);   // open files
    fdw = open("gg.txt", O_WRONLY | O_CREAT);
    if (fdr < 0 || fdw < 0) {
        perror("failed to open input or output files");
        exit(EXIT_FAILURE);
    }

    while (read(fdr, &c, 1)) { // read/write a single char from/to the files
        if (c != ' ' && c != EOF) {
            if (write(fdw, &c, 1) != 1) {
                perror("write() failed");
                exit(EXIT_FAILURE);
            }
        }    // echo char to stdout
    }

    close(fdr);   // close the files
    close(fdw);
    exit(EXIT_SUCCESS);
}

编辑部分

再次嘿,

我设法读取文件并将其写入没有空格的新文件中,但是我试图将值插入矩阵,但是尝试打开新文件时出现错误。

我已更改权限

fdw = open("gg.txt", O_RDWR | O_CREAT | O_TRUNC, 0644);

功能

void removeSpaces(int matrix[SIZE][SIZE],int fdr, int fdw) {

    char c;
    char matBuffer={0};
    while (read(fdr, &c, 1))          // read/write a single char
    {                                  // from/to the files
        if (c != ' ') {
            if (write(fdw, &c, 1) != 1) {
                perror("write() failed");
                exit(EXIT_FAILURE);
            }
        }
    }
    int i;
    int j;
    int k=0;
    while(read(fdw, &matBuffer, 1))
    {
        for(i=0;i<SIZE;i++)
        {
            for(j=0;j<SIZE;j++)
                    {
                        matrix[i][j]=matBuffer-'0';
                        k++;
                    }
            k=0;
        }

    }

}

整个程序

// C program to illustrate
// open system call
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/types.h>
#include <unistd.h>
#include <ctype.h>
#include <fcntl.h>
#define MAX_LEN 18
#define SIZE 9
void removeSpaces(int matrix[SIZE][SIZE],int fdr, int fdw) {

    char c;
    char matBuffer={0};
    while (read(fdr, &c, 1))          // read/write a single char
    {                                  // from/to the files
        if (c != ' ') {
            if (write(fdw, &c, 1) != 1) {
                perror("write() failed");
                exit(EXIT_FAILURE);
            }
        }
    }
    int i;
    int j;
    int k=0;
    while(read(fdw, &matBuffer, 1))
    {
        for(i=0;i<SIZE;i++)
        {
            for(j=0;j<SIZE;j++)
                    {
                        matrix[i][j]=matBuffer-'0';
                        k++;
                    }
            k=0;
        }

    }

}
int is_safe(int matrix[9][9],int n, int r, int c)
{
    int i,j;
    //checking in row
    for(i=0;i<9;i++)
    {
        //there is a cell with same value
        if(matrix[r][i] == n)
            return 0;
    }
    //checking column
    for(i=0;i<SIZE;i++)
    {
        //there is a cell with the value equal to i
        if(matrix[i][c] == n)
            return 0;
    }
    //checking sub matrix
    int row_start = (r/3)*3;
    int col_start = (c/3)*3;
    for(i=row_start;i<row_start+3;i++)
    {
        for(j=col_start;j<col_start+3;j++)
        {
            if(matrix[i][j]==n)
                return 0;
        }
    }
    return 1;
}
int main(int argc, char * argv[]) {
    int pipe_descs[2];
    int matrix[SIZE][SIZE];
    int fdr, fdw;   // file descriptors
    int i,j;
/*  if (pipe(pipe_descs) == -1) {
        fprintf(stderr, "cannot open");
        exit(1);
    }
    pid_t status = fork();
    if(status ==0 )
    {

    }*/
    fdr = open(argv[1], O_RDONLY);   // open files
    fdw = open("gg.txt", O_RDWR | O_CREAT | O_TRUNC, 0644);
    if (fdr < 0 || fdw < 0) { //validation for error
        perror("failed to open input or output files");
        exit(EXIT_FAILURE);
    }
    removeSpaces(matrix,fdr, fdw);
    for(i=0; i<9; i++){    /* Iterate of each row */
        for(j=0; j<9; j++){    /* In each row, go over each col element  */
          printf("%c ",matrix[i][j]); /* Print each row element */
        }
        printf("\n");        /* Finish a row, start a new line */
      }
    close(fdr);   // close the files
    close(fdw);

    exit(EXIT_SUCCESS);
}

1 个答案:

答案 0 :(得分:1)

代码中存在多个问题:

  • 您无需测试read错误,read可以返回-1进行错误检查,这不会停止while循环。
  • 测试c != EOF是没有意义的。 EOF返回getc()以指示流结束或输入错误,read指示其返回值中的这些条件,如果{{1} }成功并返回c
  • read不会被带有给定标志的1截断。您可能会覆盖文件的开头,并且由于某种原因,文件比以前的尝试更长,并且仍然包含以前写入的数据。您还必须将用于文件创建的模式位作为第三个参数传递到gg.txt。使用这个:

    open
  • 注释open似乎没有引用任何代码。

这是更正的版本:

// open the file for writing, truncate if it exists or create with
// read/write permission for user, read permission for group and others
fdw = open("gg.txt", O_WRONLY | O_CREAT | O_TRUNC, 0644);