[英]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.