2017-10-27 24 views
1

私は、cでプログラミングするのが本当に新しいと言って始めます。ヘッダーファイルとファイル操作の作成方法

ok ....質問は同じではありません...私はプログラムがファイルを開き、それ以上何もしませんが、かなり良く見える多くのものを修正しました。私は束警告を持っていますが、エラーはありません。あなたは私のコードを見て、私を助けてくれますか?ありがとう

#include <stdio.h> 



///////////////////////////////////////////////////////////////////////////////////////////////////////////////// 
//                            // 
//    Definitions - Informations to the preprocessor             // 
//                            // 
///////////////////////////////////////////////////////////////////////////////////////////////////////////////// 

#define MAX_SIZE 1024 
#define ERROROF "Could not open the file!!!" 
#define SUCCESS "file sucessefuly opened!!!" 


///////////////////////////////////////////////////////////////////////////////////////////////////////////////// 
//                            // 
//    Functions - Implementation of several functions            // 
//                            // 
///////////////////////////////////////////////////////////////////////////////////////////////////////////////// 

int counting_lines(int *ptr){ 
char c; 
int lines=0; 

    do 
    { 
     c=fgetc(ptr); 


     if (c="\n"){ 
      lines++; 
     } 
    } 
    while((c!=EOF)); 

    return lines; 
} 


int freading_diff(int *ptr, int a, int numb_lines_total) /* Function definition */ 
{ 
    char *lines2ptr; 
    char lines2[numb_lines_total-2*a]; 

    char buffer[MAX_SIZE]; 

    int i; 

    for (i=0; i<numb_lines_total-2*a; i++){ 
    lines2[i]=fgets(buffer, MAX_SIZE, ptr); 
} 
    return &lines2ptr; 

} 



int freading_common(int *ptr1, int *ptr2, int a) /* Function definition */ 
{ 
    char *lines1ptr; 
    char lines1[2*a]; 
    lines1ptr=lines1[0]; 
    char buffer[MAX_SIZE]; 

    int i=0; 

    for (i=0; i<a; i=i+2){ 
    lines1[i]=fgets(buffer, MAX_SIZE, ptr1); 
    lines1[i+1]=fgets(buffer, MAX_SIZE, ptr2); 
    } 
return lines1ptr; 

} 


void screen_printing(char *lines1ptr, char *lines2ptr, int a, int numb_lines_total) 
{ 
    int i=0; 

    for (i=0; i<2*a; i++) 
    { 
     printf(lines1ptr+i); 
    } 
    for (i=0; i<numb_lines_total-2*a; i++) 
    { 
     printf(lines2ptr+i); 
    } 
} 


void main(void) 

{ 

///////////////////////////////////////////////////////////////////////////////////////////////////////////////// 
//                            // 
//            Opening the files            // 
//                            // 
///////////////////////////////////////////////////////////////////////////////////////////////////////////////// 

    FILE *ptr1; 
    FILE *ptr2; 
    char filename1[255];   //variable to store the name of the first file 
    char filename2[255]; 
    printf("First file : ");  //prompts the user about the name of the first file 
    scanf("%63s", filename1); 
    printf("Second file : ");  //prompts the user about the name of the first file 
    scanf("%63s", filename2); 

    if((ptr1 = fopen(filename1,"r")) == NULL) //checks if the first file can be opened 
    { 
     printf("%s %s \n",ERROROF, filename1); 
     return -1; 
    } else printf("%s %s ADDRESS %d\n",SUCCESS, filename1, &ptr1); 
    if((ptr2 = fopen(filename2,"r")) == NULL) //checks if the first file can be opened 
    { 
     printf("%s %s \n",ERROROF, filename2); 
     return -1; 
    }else printf("%s %s ADDRESS %d\n",SUCCESS, filename2, &ptr2); 


    int numb_lines_total=0; 

    int a=0, b=0; 

    int lines1=0, lines2=0; 

    int *pointerline1, *pointerline2; 

    int readlines1, readlines2; 
    pointerline1=&readlines1; 

    pointerline2=&readlines2; 
    lines1=counting_lines(ptr1); 
    lines2=counting_lines(ptr2); 
    numb_lines_total = lines1+lines2; 
    printf("First file has %d lines. Second file has %d lines. %d lines will be printed on the screen\n",lines1, lines2, numb_lines_total); 

    if (lines1<lines2) 
     { 
     a= lines1; 
     b= 2*a+1; 
     readlines1=freading_diff(ptr2, a, numb_lines_total); 
     } else if (lines1>lines2) 
       { 
        a= lines2; 
        b= 2 * a + 1; 
        readlines1=freading_diff(ptr1, a, numb_lines_total); 
       } else { 
         a= lines1; 
         } 
    readlines2=freading_common(ptr1, ptr2, a); 
    screen_printing(pointerline1, pointerline2, a, numb_lines_total); 


    fclose(ptr1); 
    fclose(ptr2); 


    return; 
} 
+2

'char x'はファイル名全体ではなく、単一のcharです。 'fopen'をチェックすると' const char * filename'パラメータが使われます。おそらくあなたの機能もそうするべきです。 –

答えて

0

あなたのファンクション機能はファイルをオープンしていますが、何もしていません。 fp変数は、プログラムが関数の実行を終了するとすぐに消えるローカル変数です。

"fopen"関数は、ファイルの先頭へのポインタを返します。ファイルを読み込むには、そのポインタが必要です。

それ以外に、私はあなたが何を望んでいるのか本当に分かっていないので、それをよりよく指定してください。

何が問題ですか?コードはコンパイルされますか?それは1か0を印刷しますか?

あなたは本当に他のトピックを作成する必要があります/あなたの投稿を編集して、より多くのことを求める別の回答を追加するのではなく、 あなたはこの "-1073741819(0xC0000005)**"の意味を誰もが知っていると思いますか? どのようにそのメモリ違反を知っていますか?それはエラーを印刷しましたか?あなたはvalgrindを実行しましたか?私はそれを知らない、あなたは本当にここに投稿する方法を学ぶべきである。

それ以外は、あなたのコードがEOFを印刷しようとしていると思います。

file.txt 
a 
b 
c 
EOF 

1º loop - is scanf != EOF? yes, loop again 
    print "a" 
2º loop - is scanf != EOF? yes, loop again 
    print "b" 
3º loop - is scanf != EOF? yes, loop again 
    print "c" 
4º loop - is scanf != EOF? no. Do not loop again 
    print "???" 

エラー。

+0

私が本当に欲しかったのは(今のところ)ファイルを開くだけです(私はファイルを閉じるのを忘れていることに気付きました)。ファイルが開かれたかどうかを確認するために0または1を出力します。 –

+0

私はあなたのコードの唯一の問題は、あなたがfopeningのためのパラメータとしてcharを宣言し、それに文字列を渡しているということです。私はあなたのコードの私のバージョンを行って、それは動作します –

+0

"EOF"を印刷しようとするのをやめるなら、 –

関連する問題