Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C-仅从file.txt读取奇数行的程序_C_Arrays_File - Fatal编程技术网

C-仅从file.txt读取奇数行的程序

C-仅从file.txt读取奇数行的程序,c,arrays,file,C,Arrays,File,我对程序的实现有一个问题,它将有3个功能:void read(char*文件名(程序将从该文件中读取并从终端中的奇数行打印文本)…、void write(char*文件名1(程序将从输入文件中的奇数行中写入字符数)、char*文件名2(程序将从输入文件中的行中写入二进制数的字符,这些行有奇数)…)和main,main将这些文件的名称作为参数。然后我将能够使用如下方式启动程序:./a.out input.txt output.txt output.bin 在main函数中我有char数组[10][

我对程序的实现有一个问题,它将有3个功能:void read(char*文件名(程序将从该文件中读取并从终端中的奇数行打印文本)…、void write(char*文件名1(程序将从输入文件中的奇数行中写入字符数)、char*文件名2(程序将从输入文件中的行中写入二进制数的字符,这些行有奇数)…)和main,main将这些文件的名称作为参数。然后我将能够使用如下方式启动程序:./a.out input.txt output.txt output.bin 在main函数中我有char数组[10][80]。总之,我想从输入文件.txt中读取前10行,然后用奇数索引在终端中写入这些行,然后将这些行中的字符数(作为文本和二进制)保存在两个文件中:.txt和.bin

#include <stdio.h>
#include <string.h>
#define N 10
#define M 80


void read(char *file_name){
FILE *file_name;
file_name=fopen(".txt", "r");
char tab[80];
if (input==NULL) {
        printf("Error opening file");
        exit(-1);
        }
while(!feof(input)){
    fgets(tab, 80, input);
 }
}

void write(){
}


int main(int argc, char *argv[]){
char TEKST[N][M];
read(argv[1]);
write(argv[2], argv[3]);

return 0;
}
#包括
#包括
#定义n10
#定义M 80
无效读取(字符*文件名){
文件*文件名;
文件名=fopen(“.txt”,“r”);
字符标签[80];
如果(输入==NULL){
printf(“打开文件时出错”);
出口(-1);
}
而(!feof(输入)){
fgets(选项卡,80,输入);
}
}
无效写入(){
}
int main(int argc,char*argv[]){
char-TEKST[N][M];
read(argv[1]);
写入(argv[2],argv[3]);
返回0;
}
这是我开始编写的代码。我不知道如何将输入文件名作为参数传递给fopen函数,如何打印输入文件中带有奇数索引文本的纯终端行,以及如何计算这些行中的字符数并将它们保存到2个文件:.txt和.bin

#include <stdio.h>
#include <string.h>
#define N 10
#define M 80


void read(char *file_name){
FILE *file_name;
file_name=fopen(".txt", "r");
char tab[80];
if (input==NULL) {
        printf("Error opening file");
        exit(-1);
        }
while(!feof(input)){
    fgets(tab, 80, input);
 }
}

void write(){
}


int main(int argc, char *argv[]){
char TEKST[N][M];
read(argv[1]);
write(argv[2], argv[3]);

return 0;
}

提前感谢!

我想你会得到第一行,将它放入输出字符串,然后得到下一行并丢弃它。重复这个过程直到文件结束

大概是这样的:

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

int main()
{
    //open the file
    FILE * file;
    file = fopen("file.txt", "r");

    //the max number of characters per line
    const int maxCharsPerLine = 100; //you can change this number to whatever you want

    //the maximum amout of characters you want on the output
    const int maxOutputChars = 1000; //you can change this number to whatever you want

    //a c string to hold a single line of data at a time
    char currLine[maxCharsPerLine];

    //a c string to hold the odd lines of data
    char oddLines[maxOutputChars];
    memset(oddLines, 0, sizeof(oddLines));

//a c string to hold the even lines of data
char evenLines[maxCharsPerLine];

//while you are not at the end of the file
while (/*get a line of characters*/ fgets(currLine, maxCharsPerLine, file) != NULL)
{
    //update the line of characters
    strcat(oddLines, currLine);

    //discard a line of characters
    fgets(evenLines, maxCharsPerLine, file);
    memset(evenLines, 0, sizeof(evenLines));
}
fclose(file);

//print the odd lines
printf("%s", oddLines);
}
#包括
#包括
int main()
{
//打开文件
文件*文件;
file=fopen(“file.txt”、“r”);
//每行的最大字符数
const int maxCharsPerLine=100;//您可以将此数字更改为您想要的任何数字
//输出中所需的最大字符数
const int maxOutputChars=1000;//您可以将此数字更改为您想要的任何数字
//一次保存一行数据的c字符串
char currLine[maxCharsPerLine];
//保存奇数行数据的c字符串
字符oddLines[maxOutputChars];
memset(oddLines,0,sizeof(oddLines));
//保存偶数行数据的c字符串
半焦均匀线[maxCharsPerLine];
//当您不在文件末尾时
while(/*获取一行字符*/fgets(currLine、maxCharsPerLine、file)!=NULL)
{
//更新字符行
strcat(奇数行、当前行);
//丢弃一行字符
FGET(均匀线、maxCharsPerLine、文件);
memset(evenLines,0,sizeof(evenLines));
}
fclose(文件);
//打印奇数行
printf(“%s”,奇数行);
}

Hi!您的问题是…?…请发布您已经编写的代码。代码从何处开始?1.
FILE*FILE\u name;
-使用与函数参数相同的变量名。2.不检查返回值-是否打开了文件?3您确定要打开的文件名为
.txt
。调用在io.4中使用函数
read
input
-这是在哪里声明的?5.这是否编译…嗨,Joshua-欢迎使用StackOverflow。根据这个问题的编写方式,我想这个用户需要一些代码本身的帮助,而不仅仅是算法方面的帮助。如果你能提供帮助的话(请记住对问题的评论)那么请这样做。