2017-11-27 25 views
-2

プログラムの実装に問題があり、3つの関数があります:void read(char * file name(プログラムはそのファイルから読み込み、 char * file_name1(プログラムは入力ファイルから奇数番号の行から文字数を書き出します)、char * file_name2(プログラムは行から2進数の文字を書きます。入力ファイル - 奇数番号を持つ)...)とmainで、これらのファイルの名前を引数として持ちます。次に、私はこのようなプログラムを起動することができます:./a.out input.txt output.txt output.bin 主な機能では、私はchar配列[10] [80]を持っています。要約すると、私は入力ファイル.txtから最初の10行を読み込み、次に奇数のインデックスを持つ行を端末に書き込み、これらの行に.txtと.binの文字数(テキストとバイナリとして)を保存します。C - file.txtから奇数行だけを読み取るプログラム

#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; 
} 

これは私が書き始めたコードです。私は引数としてfopen関数に入力ファイル名を渡す方法、入力ファイルからの奇数のインデックスを持つテキストの行だけを出力する方法を知らない。そして、これらの行から文字を数えて、.txtと.binの2つのファイルに保存する方法。

ありがとうございます!

+0

こんにちは!あなたの質問は...? ... [良い質問をする方法](https://stackoverflow.com/help/how-to-ask) – purplepsycho

+0

あなたがすでに書いたコードを投稿してください。 –

+0

どこから始めますか? 1. 'FILE * file_name;' - 関数の引数と同じ変数名を使用する。 2.戻り値をチェックしていない - ファイルを開いたことがありますか? 3開きたいファイルが '.txt'であることを確かめますか?これがioの場合には関数 'read'を呼び出します。 4. 'input' - これはどこに宣言されていますか? 5.これはコンパイルされますか? –

答えて

-1

私はあなたが最初の行を取得し、出力文字列に入れ、次の行を取得して破棄すると思います。ファイルが終了するまでそれを繰り返します。このような

何か:

#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); 
} 
+0

こんにちはJoshua - StackOverflowへようこそ。この質問が書かれている方法に基づいて、このユーザーはアルゴリズムだけでなくコードそのものにいくつかの助けが必要だと思います。あなたがそれを助けることができるなら(質問のコメントに気をつけて)、してください。 –

関連する問題