c - 如何解决此打印输出问题?

标签 c operating-system printf

对于家庭作业,我用 C 编写了一些代码来打印存储在 achive 文件中的文件的权限和文件名。我的代码可以编译,但由于某种原因,第二个文件的名称在新行中返回。

这是我的输出在终端中的样子:

rw-r--r-- 501/20 1-s.txt        
rw-r--r-- 501/20 
2-s.txt       
(empty blank line here)

这就是我希望输出的样子:

rw-r--r-- 501/20 1-s.txt        
rw-r--r-- 501/20 2-s.txt 
(no empty blank line here)

有人可以看一下我的代码并给我一些关于如何修复我的代码的提示吗?我怀疑这与我读取和选择文件名的方式有关(使用 my_ar.ar_name),但我不知道如何修复它。感谢您抽出时间。

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/utsname.h>
#include <ctype.h>
#include <string.h>
#include <ar.h>


int main (int argc, char **argv)
{
    FILE *fp;
    size_t readNum;
    long long tot_file_size, cur_file_size;
    struct stat fileStat;
    struct ar_hdr my_ar;


    //open the archive file (e.g., hw.a)
    fp = fopen(argv[1], "r");
    if (fp == NULL)
    {
        perror("Error opening the file\n");
        exit(-1);
    }

    //size of the archive file
     fseek(fp, 0, SEEK_END);
     tot_file_size =ftell(fp);
     rewind(fp);


    //read data into struct
    fseek(fp, strlen(ARMAG), SEEK_SET); 

    file_size_cnt = 0;
    while (ftell(fp) < tot_file_size - 1)
    {
        readNum = fread(&my_ar, sizeof(my_ar), 1, fp);

        if (stat(argv[1], &fileStat) == -1) {
            perror("stat");
            exit(EXIT_FAILURE); 
}


        if ((fileStat.st_mode & S_IFMT) == S_IFREG)
        {

            printf( (fileStat.st_mode & S_IRUSR) ? "r" : "-");
            printf( (fileStat.st_mode & S_IWUSR) ? "w" : "-");
            printf( (fileStat.st_mode & S_IXUSR) ? "x" : "-");
            printf( (fileStat.st_mode & S_IRGRP) ? "r" : "-");
            printf( (fileStat.st_mode & S_IWGRP) ? "w" : "-");
            printf( (fileStat.st_mode & S_IXGRP) ? "x" : "-");
            printf( (fileStat.st_mode & S_IROTH) ? "r" : "-");
            printf( (fileStat.st_mode & S_IWOTH) ? "w" : "-");
            printf( (fileStat.st_mode & S_IXOTH) ? "x" : "-");

            printf("%.*s", 15, my_ar.ar_name);
            printf("\n");

        }

        cur_file_size = atoll(my_ar.ar_size);

        if (fseek(fp, cur_file_size, SEEK_CUR) != 0)
        {
            perror("You have an error.\n");
            exit(-1);
        }

    }

    //printf("\n");
    fclose(fp);

    return 0;
}

最佳答案

您不想打印新行吗?所以不要打印它!

删除这个:

printf("\n");

它位于代码末尾。

编辑

我猜你不想完全删除它,但你想在除了最后一次之外的任何时候打印它。

if (ftell(fp) != tot_file_size - 1) {
    printf("\n");
}

编辑V1.1

经过改进后,我在我的 linux mint 13 下运行了你的代码。这是我的输出:

rw-r--r--a.txt/         
rw-r--r--b.txt/ 

这就是您想要的格式,不是吗?

关于c - 如何解决此打印输出问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17751517/

相关文章:

iPhone OS 3.1.3 不适用于 Xcode 3.2.1。我必须安装 SDK 3.2 BETA 吗?

linux - 如何动态分配 CPU 资源给 Linux 中的进程?

正则表达式精确匹配

c - printf() 在命令行中重定向到文件 (Cygwin)

c - 从 C 中的 argv[i] 检查 fopen 时出错

c - 使: create a test binary for each file in test directory

linux - sem_init 和 sema_init 的区别

c - 将字符串分配给 int 并将该 int 传递给 printf 如何正确打印字符串?

c - 如何打印一个 int struct 成员

c++ - 可以将非常量字符数组传递给常量字符数组吗?