[英]How do I run C code on linux with input file from command line?
I'm trying to do some simple tasks in C and run them from the command line in Linux.我正在尝试在 C 中执行一些简单的任务,并从 Linux 中的命令行运行它们。 I'm having some problems with both C and running the code from the command line with a given filename given as a parameter.我在 C 和使用给定文件名作为参数的命令行运行代码时遇到了一些问题。 I've never written code in C before.我以前从未在 C 中编写过代码。
Remove the even numbers from a file.从文件中删除偶数。 The file name is transferred to the program as a parameter in the command line.文件名作为命令行中的参数传递给程序。 The program changes this file.程序更改此文件。
How do I do these?我该怎么做?
my attempt at writing the c code:我尝试编写 c 代码:
#include <stdio.h>
int main ()
{
FILE *f = fopen ("arr.txt", "r");
char c = getc (f);
int count = 0;
int arr[20];
while (c != EOF)
{
if(c % 2 != 0){
arr[count] = c;
count = count + 1;
}
c = getc (f);
}
for (int i=0; i<count; i++){
putchar(arr[i]);
}
fclose (f);
getchar ();
return 0;
}
I'd do that like this (removing extra declarations => micro optimizations)我会这样做(删除额外的声明=>微优化)
/**
* Check if file is avaiable.
*/
if (f == NULL)
{
printf("File is not available \n");
}
else
{
/**
* Populate array with even numbers.
*/
while ((ch = fgetc(f)) != EOF)
ch % 2 != 0 ? push(arr, ch); : continue;
/**
* Write to file those numbers.
*/
for (int i = 0; i < 20; i++)
fprintf(f, "%s", arr[i]);
}
Push implementation:推送实现:
void push(int el, int **arr)
{
int *arr_temp = *arr;
*arr = NULL;
*arr = (int*) malloc(sizeof(int)*(n - 1));
(*arr)[0] = el;
for(int i = 0; i < (int)n - 1; i++)
{
(*arr)[i + 1] = arr_temp[i];
}
}
In order to write to the same file, without closing and opening it, you should provide both methods, w+ (writing and reading), and this method will clear it's content.为了写入同一个文件,而不是关闭和打开它,你应该提供两个方法,w+(写入和读取),这个方法会清除它的内容。
So, change the line where you open the file, for this.因此,为此更改打开文件的行。
FILE *f = fopen ("arr.txt", "w+");
You should look for ways of implementing dynamic arrays (pointers and memory management).您应该寻找实现动态 arrays(指针和 memory 管理)的方法。
With this example you could simply go ahead and write yourself, inside the main loop, a temporary variable that stores a sequence of numbers, and stack those values在这个例子中,你可以简单地提前 go 并在主循环中自己编写一个存储数字序列的临时变量,并将这些值堆叠起来
Something like this (pseudocode, have fun:)):像这样的东西(伪代码,玩得开心:)):
DELIMITER one of (',' | '|' | '.' | etc);
char[] temp;
if(ch not DELIMITER)
push ch on temp;
else
push temp to arr and clear it's content;
Hope this was useful.希望这很有用。
Here's a complete program which meets your requirements:这是一个满足您要求的完整程序:
int main(int argc, char *argv[])
, the first parameter is in argv[1]
if argc
is at least 2.通过参数给出文件名- 如果定义int main(int argc, char *argv[])
,如果argc
至少为 2,则第一个参数位于argv[1]
中。#include <stdio.h>
#include <ctype.h>
#include <unistd.h>
int main(int argc, char *argv[])
{
if (argc < 2) return 1; // no argument given
FILE *f = fopen(argv[1], "rb+");
if (!f) return 1; // if fopen failed
// read, write and number position
long rpos = 0, wpos = 0, npos = 0;
int even = 0, c; // int to hold EOF
while (c = getc(f), c != EOF)
{
if (isdigit(c)) even = c%2 == 0;
else
{
if (even) wpos = npos, even = 0;
npos = wpos+1; // next may be number
}
fseek(f, wpos++, SEEK_SET);
putc(c, f);
fseek(f, ++rpos, SEEK_SET);
}
ftruncate(fileno(f), wpos); // shorten the file
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.