Thread: FILE *streamname as a public member of a class.

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    10

    FILE *streamname as a public member of a class.

    Okaay. At this spring I decided to start learning C++. C I knew somehow before... Now I'm doing a lil proggie to help me with updating my webpages... And I encounter a prob.

    I have a class for fileoperations, and it has couple of

    FILE *name (Is that variable? stream? I assume you know what I mean... English is not my native language )

    Okay, then I have public functions for handling stuff in the files, like one for replacing certain occurances from the file with some txt. Now when I try to read the file with fgets, it crashes my proggie O_o

    Compiler does not spot errors, but program crashes on the fgets line.

    It is something like that:

    class (in header)
    Code:
    .
    .
    .
    #ifndef _FILEOP_H
    #define _FILEOP_H
    
    class fileop {
    private:
    public:
    .
    char path[100];
    char filename[50];
    char wholename[151];
    .
    FILE *reading,*writing,*creating,*temporary;
    .
    .
    .
    };
    And in fileop.cpp I have functions
    Code:
    .
    .
    .
    int fileop::replace(char to_be_replaced[],char replacement[]){
    char line[2000],temp[151]; //temp is created later from wholename by changing last letter
    .
    .
    .
    reading = fopen(wholename,"r");
    //this one works just fine.
    .
    .
    temporary=fopen(temp,"w");
    .
    fgets(line,1999,reading); //at this point I do receive errormessage :|
    .
    .
    .
    I also had problems with checking if the reading was already open (differed from the null) before opening the file. So I just skipped that part.

    Is there some dark C/C++ ghost, backstabbing us poor noobs, or is there a real explanation?

  2. #2
    Registered User Mortissus's Avatar
    Join Date
    Dec 2004
    Location
    Brazil, Porto Alegre
    Posts
    152
    Are the file names initialized? Please, show all your code.
    Test for NULL FILE* pointers. You could also consider using fstream (C++ files).

  3. #3
    Registered User
    Join Date
    Apr 2005
    Posts
    10
    initialized.. Another fancy term ^_^

    (I am a noob in C++, and if not exactly a noob in C, at least a beginner )

    Whole code...

    I can do it, but it is written in Finnish, which will make it hard to follow :| Well, anyways, I'll put it in here... I'll do translation someday, if I do have time Thanks for your intrest anyways

    tiedosto.h (fileop.h)
    [code]
    /*

    Sisältää yleisen tiedosto luokan.
    (contains introduction of tiedosto [file] class)


    */

    #ifndef _TIEDOSTO_H
    #define _TIEDOSTO_H
    class tiedosto
    {
    private:
    public:

    tiedosto(char nim[], char pol[]); //luo uuden olion (constructor)
    ~tiedosto(); //destruktori (destructor)
    int polunmitta; //path lenght
    int nimenmitta; //name lenght
    int kokomitta; //path+name lenght
    char nimi[50]; //name (of the file)
    char polku[100]; //name of the path
    char kokonimi[151]; //path+name
    FILE *ylikirjoita, *lisaa, *lue, *valiaikainen; //filestreams

    int Alusta(); //alustaa (luo uuden tyhjan tiedoston palauttaa 1 jos onnistuu, 0 jos ei)
    // formats a file (creates empty file. DOES not work :| Why? returns 1 if success, 0 if not.
    int korvaa(char korvattava_teksti[],char korvaava_teksti[]);//palauttaa korvattujen lkmn, -1 jos epaonnistui.
    //replaces a certain occurrance (korvattava_teksti[]) from the file with korvaava_teksti[]
    //returns amount of replaced words, -1 if fails.
    /* int lisaa_alkuun(char lisayskohta[],char lisattava[]);
    int lisaa_loppuun(char lisayskohta[], char lisattava[]); */
    int tarkista_lue();// palauttaa 0 jos kiinni, 1 jos auki
    int tarkista_lisaa();// palauttaa 0 jos kiinni, 1 jos auki
    int tarkista_ylikirjoita();// palauttaa 0 jos kiinni, 1 jos auki
    int tarkista_valiaikainen();// palauttaa 0 jos kiinni, 1 jos auki
    int close_all(); //palauttaa 1 jos onnistuu, -1 jos ei




    };

    #endif
    [/colde]

    tiedosto.cpp (fileop.cpp)

    Code:
    #include <iostream.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <ctype.h>
    #include <process.h>
    /*#include <unistd.h>*/
    #include "tiedosto.h"
    
    /* 
    Tiedostojen kopiointiin (to copy files)
    */
    void copy(FILE *source,FILE *target);
    
    
    /***********************************************************
    konstruktori constructor, takes name&path as arguments
    ************************************************************/
    
    tiedosto::tiedosto(char nim[], char pol[]){  
           int i=0;
           polunmitta = strlen(pol);
           nimenmitta = strlen(nim);
           
           if( nimenmitta > 51 ){
                    cout << "Tiedoston nimi liian pitka\n" << endl;
                    exit(1);
           }
           if( polunmitta > 101 ){
                    cout << "Tiedoston polku liian pitka\n" << endl;
                    exit(1);
           }
           nim[nimenmitta]='\0';
           pol[polunmitta]='\0';
           polunmitta = strlen(pol);
           nimenmitta = strlen(nim);
           kokomitta=nimenmitta+polunmitta;
           strcpy(nimi,nim);
           strcpy(polku,pol);
           for(i=0;i<=polunmitta;i++){
                    kokonimi[i]=pol[i];
           }
           for(i=0;i<=nimenmitta;i++){
                    kokonimi[polunmitta+i]=nim[i];
           }
           cout << polku << nimi << endl;
           cout << "tiedosto nimelta " << kokonimi << "luotu" << endl;
    }
    
    
    
    
    /***********************************************************
    destruktori (destructor)
    ************************************************************/
    
    tiedosto::~tiedosto(){
                          delete [] nimi;
                          delete [] polku;
                          delete [] kokonimi;
    }
    
    
    
    /***********************************************************
    Alusta  (format = creates a empty file) For some reason does not work.
    ************************************************************/
    
    int tiedosto::Alusta(){
        cout << kokonimi << endl;
        getchar();
            
        ylikirjoita=fopen(kokonimi,"w");
        if( ylikirjoita == NULL ){
            cout << "alustus epaonnistui\n" << endl;
            return -1;
        }
        else{
            fclose(ylikirjoita);
             return 1;
        }
    }
    
    
    /***********************************************************
    Tiedostojen tilan tarkistus
    ************************************************************/
    
    int tiedosto::tarkista_lisaa(){
        if( lisaa == NULL ){
            return 0;
        }
        else{
             return 1;
        }
    }       
    int tiedosto::tarkista_lue(){
            if( lue == NULL ){
            return 0;
        }
        else{
             return 1;
        }
    }
    int tiedosto::tarkista_ylikirjoita(){
            if( ylikirjoita == NULL ){
            return 0;
        }
        else{
             return 1;
        }
    }
    
    
    /***********************************************************
    Sulje kaikki
    ************************************************************/
    
    int tiedosto::close_all(){
        if(lue == NULL){
            ;
        }
        else{
        fclose(lue);
        }
        if(lisaa == NULL){
            ;
        }
        else{
        fclose(lisaa);
        }
        if(ylikirjoita == NULL){
            ;
        }
        else{
        fclose(ylikirjoita);
        }
        if(valiaikainen == NULL){
            ;
        }
        else{
        fclose(valiaikainen);
        }
    	if(lue==NULL&&lisaa==NULL&&ylikirjoita==NULL&&valiaikainen==NULL){
    		return 1;
    	}
    	else{
    		return -1;
    	}
    
    }
    
    /***********************************************************
    korvaaminen
    ************************************************************/
    
                                   
    int tiedosto::korvaa(char korvattava[],char korvaava[]){
        char rivi[2000];
        int testi,i=0,x,j=0,rivimitta=0,korvattu=0,korvattava_mitta,korkohta=0,apuindeksi=0,korvaavamitta,korvattavamitta,korvattuja;
        char valiaika[152]={'\0','\0'};
        korvattavamitta=strlen(korvattava);
        korvaavamitta=strlen(korvaava);
        /*
        cout << "korvauksessa, tarkistetaan onko lue auki";
        getchar();
        if( lue == NULL ){
        */
            /*cout << "korvauksessa, AvataanLue. Tiedostonnimi " << kokonimi << endl;
            testi=strlen(kokonimi);
            cout << "viimeinen merkki " << kokonimi[testi-1] << endl;
    		kokonimi[testi]='\0';
        getchar();
        fflush(stdin);*/
            lue = fopen(kokonimi,"r");
        /*
        }
        */
        if( lue == NULL ){
            cout << "tiedoston " << kokonimi << "avaus epaonnistui" << endl;
            return -1;
            }
        strcpy(valiaika,kokonimi);
             
        valiaika[kokomitta-1]='$';
        valiaikainen=fopen(valiaika,"w");
          if( valiaikainen == NULL ){
            cout << "tiedoston " << valiaika << "avaus epaonnistui" << endl;
            return -1;
        }
        while(feof(lue)==0){
            rivimitta=strlen(rivi);
            for(i=0;i<rivimitta;i++){
                rivi[i]='\0';
            }
            
            fgets(rivi,1999,lue); //program crashes to here...
            for(i=0;i<rivimitta;i++){//muutettava!
                if(rivi[i]==korvattava[0]){
                    for(j=0;rivi[i+j]==korvattava[j]&&korvattu!=1;j++){
                        if(j==korvattavamitta-1&&korvattu!=1){
                             fprintf(valiaikainen,"%s",korvaava);//tarkista syntaksi
                              korvattuja++;
                              korvattu=1;
                              i=i+korvattavamitta;
                        
                        }
                    }
                }
                if(korvattu==0){ 
                      fputc(rivi[i],valiaikainen);
                }
                korvattu=0;
            }
        }
        fclose(lue);
        fclose(valiaikainen);
        ylikirjoita=fopen(kokonimi,"w");
        if( ylikirjoita == NULL ){
            cout << "tiedoston " << kokonimi << "avaus epaonnistui" << endl;
            return -1;
            }
        valiaikainen=fopen(valiaika,"r");
        if( valiaikainen == NULL ){
            cout << "tiedoston " << valiaika << "avaus epaonnistui" << endl;
            return -1;
        }
        copy(valiaikainen,ylikirjoita);
        fclose(lue);
        fclose(valiaikainen);
        if(unlink(valiaika)!=0){
            cout << "virhe kopio tiedoston poistossa " << errno << endl;
            return -1;
        }
        else{
            return korvattuja;
        }
    }
    
    
    
    
    
    
    
    
    
    void copy(FILE *source,FILE *target)
    {
    int charact;
    charact = fgetc(source);
    while(charact != EOF)
    {
    fputc(charact,target);
    charact = fgetc(source);
    }
    }
    and main.cpp
    Code:
    #include <iostream.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    #include "tiedosto.h"
    
    
    main(){
    char tiedostonnimi[50]="HTTP.txt";
    char tiedostonnimi2[50]="massentesti.txt";
    char tiedostonpolku[20]="D:\\Mainio\\";
    int i;
    cout << "alku" << endl;
    getchar();
    tiedosto file1(tiedostonnimi2,tiedostonpolku);
    tiedosto file2(tiedostonnimi,tiedostonpolku);
    
    cout << "file1, file2 luotu" << endl;
    getchar();
    if(file1.Alusta()!=1){
    cout << "alustus epaonnistui" << endl;
    getchar();
    exit(1);
    }
    cout << "file1 alustus tehty" << endl;
    getchar();
    i=file2.korvaa("Maz"," <HTML> <!-- html alkaa --> ");
    cout << "html korvattu" << endl;
    getchar();
    if(i=-1){
    cout << "mitas nyt tehdaan?" << endl;
    getchar();
    exit(1);
    }
    cout << "korvattiin " << i << " kappaletta" << endl; 
    getchar();
    file1.close_all();
    file2.close_all();
    }
    I know this probably violates all "coding principles", but as I told, I am a beginner, and my main goal is to try to
    1. learn basic princibles of C++
    2. make a working proggie for updating my webpages

    Not to produce beautifull & cool code. (That'll be next step, after I've made this to work...)

    Anyways, all tips, hints etc. are appreciated, but I will be utterly gratefull if I get solution for the FILE *blaa problem...

  4. #4
    Registered User Mortissus's Avatar
    Join Date
    Dec 2004
    Location
    Brazil, Porto Alegre
    Posts
    152
    I want to help, but I don´t know how to run the software.
    I only speak portuguese, and english, and understand a bit about spanish.

    I don´t know either what the messages mean.

  5. #5
    Registered User
    Join Date
    Apr 2005
    Posts
    10
    Thanks for help Mort

    Well, the code I pasted to here was (pre) beta version So It did not work well at all, and was mainly done to test the "tiedosto" class and it's functions Well, the odd problem with FILE members remained, untill I changed the <iostream.h> to <iostream>, and added the "using namespace::std".

    Well, it seems things are working now, and my "tiedosto" class is soon ready to be used

    Thanks for your effort everyone (And if someone wishes to see the code, I can put it here.. But it's still in Finnish )

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 06-18-2009, 04:58 AM
  2. Linking problems in Visual Studio
    By h3ro in forum C++ Programming
    Replies: 5
    Last Post: 03-04-2008, 02:39 PM
  3. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM
  4. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  5. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM