如何使用来自命令行的输入文件 运行 linux 上的 C 代码?
How do I run C code on linux with input file from command line?
我正在尝试用 C 和 运行 在 Linux 中从命令行完成一些简单的任务。
我在使用 C 和 运行 从命令行使用给定文件名作为参数的代码时遇到了一些问题。我以前从未用 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.
我该怎么做?
- 从文件读取并将结果写入同一文件
- 从文件中读取数字而不是数字(例如:我需要能够读取“22”作为单个输入,而不是包含“2”的两个单独的字符)
- 通过Linux中的参数给出文件名。 (例如:./main.c file.txt)
我尝试编写 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;
}
我会这样做(删除额外声明 => 微优化)
/**
* 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]);
}
推送实施:
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];
}
}
为了写入同一个文件,而不是关闭和打开它,你应该提供两种方法,w+(写入和读取),这个方法将清除它的内容。
因此,为此更改打开文件的行。
FILE *f = fopen ("arr.txt", "w+");
你应该寻找实现动态数组的方法(指针和内存管理)。
对于这个例子,您可以简单地继续在主循环中自己编写一个存储数字序列的临时变量,并将这些值堆叠起来
类似这样的东西(伪代码,玩得开心:)):
DELIMITER one of (',' | '|' | '.' | etc);
char[] temp;
if(ch not DELIMITER)
push ch on temp;
else
push temp to arr and clear it's content;
希望这有用。
这是满足您要求的完整程序:
- 将结果写入同一文件 - 它在文件中保留读写位置,并在数字已被删除的情况下将字符复制到文件开头;最后,必须截断现在较短的文件。 (请注意,对于大文件,写入第二个文件会更有效率。)
- 从文件中读取数字而不是数字 - 不需要读取整数,存储数字的写入起始位置就足够了(这可以在每个非数字)和最后一个数字的奇偶校验。
- 通过参数给出文件名 - 如果定义
int main(int argc, char *argv[])
,第一个参数在argv[1]
中,如果argc
至少是2.
#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
}
我正在尝试用 C 和 运行 在 Linux 中从命令行完成一些简单的任务。 我在使用 C 和 运行 从命令行使用给定文件名作为参数的代码时遇到了一些问题。我以前从未用 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.
我该怎么做?
- 从文件读取并将结果写入同一文件
- 从文件中读取数字而不是数字(例如:我需要能够读取“22”作为单个输入,而不是包含“2”的两个单独的字符)
- 通过Linux中的参数给出文件名。 (例如:./main.c file.txt)
我尝试编写 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;
}
我会这样做(删除额外声明 => 微优化)
/**
* 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]);
}
推送实施:
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];
}
}
为了写入同一个文件,而不是关闭和打开它,你应该提供两种方法,w+(写入和读取),这个方法将清除它的内容。
因此,为此更改打开文件的行。
FILE *f = fopen ("arr.txt", "w+");
你应该寻找实现动态数组的方法(指针和内存管理)。
对于这个例子,您可以简单地继续在主循环中自己编写一个存储数字序列的临时变量,并将这些值堆叠起来
类似这样的东西(伪代码,玩得开心:)):
DELIMITER one of (',' | '|' | '.' | etc);
char[] temp;
if(ch not DELIMITER)
push ch on temp;
else
push temp to arr and clear it's content;
希望这有用。
这是满足您要求的完整程序:
- 将结果写入同一文件 - 它在文件中保留读写位置,并在数字已被删除的情况下将字符复制到文件开头;最后,必须截断现在较短的文件。 (请注意,对于大文件,写入第二个文件会更有效率。)
- 从文件中读取数字而不是数字 - 不需要读取整数,存储数字的写入起始位置就足够了(这可以在每个非数字)和最后一个数字的奇偶校验。
- 通过参数给出文件名 - 如果定义
int main(int argc, char *argv[])
,第一个参数在argv[1]
中,如果argc
至少是2.
#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
}