簡體   English   中英

如何創建類似於 Linux 中的 nl 命令的 C 程序?

[英]How do you create a C program that resembles the nl command in Linux?

我正在做一個項目來創建一個 C 程序,它的行為就像 Linux 中的 nl 命令,但有例外; 它只接受有限的選項,不接受破折號來表示標准輸入,並且不需要將文檔划分為帶有頁眉/頁腳的邏輯頁面。

這是 nl 手冊頁的鏈接: https://ss64.com/bash/nl.html

這就是我所在的位置。 我一生中無法修復的錯誤如下:

 Line 46: warning: implicit declaration of function 'get_line'; did you mean 'fgetline'? (-Wimplicit-function-declaration)" Line 46: warning: assignment to 'char *' from 'int' makes pointer from integer without a cast (-Wint-conversion)" Line 73: error: conflicting types for 'get_line'" Line 46: note: previous implicit declaration of 'get_line' was here." note: expected 'const char * restrict' but argument is of type 'char'"

我目前擁有的代碼:

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

//Preprocessor constants:
#define MAX_LINE_LENGTH 100

//Prototypes:
int process_stream(FILE *fpntr);
char *fgetline(FILE *fpntr);

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

    char filename;
    char *first_line;
    FILE *fpntr;

    int c;
    int lineNumber = 1;
    int beginningNum = 1;

    //Check for proper number of arguments:
    if (argc != 2)
    {
        fprintf(stderr, "Usage: firstline FILE\n");
        return EXIT_FAILURE;
    }

    //Get source and destination from arguments:
    filename = argv[1];

    //Open file for reading:
    fpntr = fopen(filename, "r");

    //Left out of body to below block:
    fprintf (stderr, "Error opening file %s: %s\n",
    filename, strerror(errno));
    return EXIT_FAILURE;
    if ((fpntr = fopen(filename, "r")) == NULL) {
        printf("cannot open file \n");
        exit(0);
    }

    //Get first line from file:
    if ((first_line = get_line(fpntr)) == NULL) {
        perror("Error reading line");
        exit(EXIT_FAILURE);
        }
    // Print out line:
    printf("First line in file %s: \n%s", filename, 
    first_line);

    //read contents from file
    while ((c = fgetc(fpntr)) != EOF){
        if (beginningNum){
            printf("%d   ", lineNumber);
            beginningNum = 0;
        }

        if (c == '\n'){
            lineNumber++;
            beginningNum = 1;
        }
            printf("%c", c);
    }

    fclose(fpntr);
    return EXIT_SUCCESS;
}
/*Function to get next line from open file and place it as 
  a legal string into
  a line buffer that is created locally and is static so it 
  can be returned.
  Returns pointer to line buffer, or else NIL on error or 
  immediate EOF.*/
char *get_line(FILE *fpntr)
{
  static char line_buff[MAX_LINE_LENGTH];

  //Get first line from file and return
  if (fgets(line_buff, MAX_LINE_LENGTH, fpntr) != NULL)
  return line_buff;
  else
  return NULL;
}
 
//EOF

我把它帶到商店並將其錘打成實用的形狀:

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

#define MAX_LINE_LENGTH 1024

int main(int argc, char* argv[]) {
    if (argc != 2) {
        fprintf(stderr, "Usage: firstline FILE\n");

        return EXIT_FAILURE;
    }

    // argv[n] is type char*
    char* filename = argv[1];

    FILE* fpntr = fopen(filename, "r");

    if (!fpntr) {
        fprintf (stderr, "Error opening file %s: %s\n", filename, strerror(errno));
        
        return EXIT_FAILURE;
    }

    if ((fpntr = fopen(filename, "r")) == NULL) {
        printf("cannot open file \n");
        exit(0);
    }

    static char line[MAX_LINE_LENGTH];

    if (!fgets(line, MAX_LINE_LENGTH, fpntr)) {
        perror("Error reading line");
        exit(EXIT_FAILURE);
    }

    printf("First line in file %s: \n%s", filename, line);

    // Try and declare variables as close as possible to where they're used
    // as it makes figuring out what they are way easier. Types are very
    // important in C!
    int begin = 1;
    size_t lineNumber = 0;
    int c;

    // Note: Would be better to read line by line here instead of individual
    //       characters, fgets has you covered, but this is your call.
    while ((c = fgetc(fpntr)) != EOF){
        if (begin) {
            // You can get printf() to do the formatting for you if you
            // let it with %-4 being 4 wide (%4), left-aligned(%-4)
            // %zu is just the type used by size_t for display
            printf("%-4zu ", lineNumber);
            begin = 0;
        }

        if (c == '\n') {
            lineNumber++;
            begin = 1;
        }

        putc(c, stdout);
    }

    fclose(fpntr);
    
    return EXIT_SUCCESS;
}

它在 My Machine™ (Clang) 上運行,除了在調試代碼后需要倒帶 ( fseek ) 以轉儲第一行之外,它看起來不錯。

總的來說,只需要輕推幾下就可以讓東西正確安裝到位。 你的想法是對的,但很多細節都只是一點點偏差。

不幸的是,在 C 中,單個字符可能是“工作正常”與痛苦的調試時間或一大堆編譯器錯誤之間的區別。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM