A little help on File reading? Not a big prob :)

This is a discussion on A little help on File reading? Not a big prob :) within the C++ Programming forums, part of the General Programming Boards category; Okay, so I'm making a decryption\encryption proggy.(only decrypts its own encryption for now). And I want it to enter the ...

  1. #1
    Registered User
    Join Date
    Dec 2001
    Posts
    206

    Angry A little help on File reading? Not a big prob :)

    Okay, so I'm making a decryption\encryption proggy.(only decrypts its own encryption for now). And I want it to enter the file, view the letters, and change them 1 by 1. Here's the code for doing it so far(yes I have defined how to get the file name):


    ofstream fout;
    ifstream fin;
    int i;

    fin.open(file);
    fin>>file;

    for(i = 0; file[i]; i++)
    {
    if(file[i] = "h")\\error occurs here
    {
    file[i] = "";
    }
    if(file[i] = "a")\\error occurs here
    {
    file[i] = "";
    }
    }

    fin.close();
    return* file;




    it gives me this error: Cannot convert char[2] to char
    twice, i have marked it in my code. Plz tell me how to fix it. thx

  2. #2
    Registered User
    Join Date
    Feb 2002
    Posts
    591
    This is not doing what you think it is doing.
    if(file[i] = "h")

    You want this
    if(file[i] == "h")

  3. #3
    Registered User
    Join Date
    Dec 2001
    Posts
    206

    omfg

    lol i didnt even notice. thank you

    PS. im not a n00b at programming. I jsut didnt see it lol

  4. #4
    Registered User
    Join Date
    Dec 2001
    Posts
    206

    well

    well there are still errors :/ four of em
    one for each file[i] = "s" and file[1] == "s" statements

  5. #5
    Registered User
    Join Date
    Feb 2002
    Posts
    591
    I know..I do it all the time. That and and the while(true);;;;;;;;;; is 75% of my syntax errors

  6. #6
    Registered User
    Join Date
    Feb 2002
    Posts
    591
    What is the error message?

  7. #7
    Registered User
    Join Date
    Dec 2001
    Posts
    206

    error messages

    the errors were:
    '=' : no conversion from 'char [2]' to 'char'
    '==' : no conversion from 'char [2]' to 'int'
    '=' : no conversion from 'char [2]' to 'char'
    '==' : no conversion from 'char [2]' to 'int'

  8. #8
    Registered User
    Join Date
    Nov 2001
    Posts
    32

    Thumbs up Switch!!

    Please use a switch case, I really seeing so many if statements..

    Switch (choice)
    {
    case 1:
    cout << "Something" << endl;
    break;
    case 2:
    cout << "Something" << endl;
    break;
    case 3:
    cout << "Something" << endl;
    break;
    default:
    break;
    }

    this looks much better!!

    Ignore this code, i didnt look to carefully at youre code, this wouldent fit in to that 'case'
    Last edited by Vinterwolf; 03-19-2002 at 02:37 PM.

  9. #9
    Registered User
    Join Date
    Feb 2002
    Posts
    591
    Try 'A' instead off "A"

  10. #10
    Registered User
    Join Date
    Dec 2001
    Posts
    206

    wow

    it worked ty barjor

  11. #11
    Registered User
    Join Date
    Dec 2001
    Posts
    206

    grahh

    now its not writing to the file!!

    ofstream fout;
    ifstream fin;
    int i;

    fin.open(file);
    fin>>file;

    for(i = 0; file[i]; i++)
    {
    if(file[i] == 'h')
    {
    file[i] = '';
    }
    if(file[i] == 'a')
    {
    file[i] = '';
    }
    fout<<file[i];
    fin.close();
    }


    fin.close();
    return* file;

    the file has "haha" inside it. When i use my program on it, i look at it and it still only says "haha"... *sniff* help plz?

  12. #12
    Registered User
    Join Date
    Dec 2001
    Posts
    206

    sry to rush but..

    sry to rush but.. im in a real big hurry if anyone knows could they plz help?

  13. #13
    Registered User
    Join Date
    Dec 2001
    Posts
    206

    Oh... and Vinter

    I suck at cases :/

    How would i make this into a working case statement:

    switch(file)
    {
    case useme[i] = 'a':
    useme[i] = '';
    break;
    }



    if it sux that bad so as u dont get it... im making it so that if the letter that useme is on is a, it changes it to :P

  14. #14
    Registered User biosx's Avatar
    Join Date
    Aug 2001
    Posts
    230
    Your code is kind of funny. First you should open the file, read the file into an array, then close the file. Then you do all your work within the array. Then when you are finished, you open another file, write to that file, then close it.

    Code:
    #include <iostream.h>   // For normal I/O
    #include <fstream.h>     // For ifstream/ofstream
    #include <string.h>        // For strlen() 
    
    int main()
    {
       char text[1024];   // Array to hold characters from infile
    
       ifstream fin("whatever.txt");   // Infile
    
       if( !fin ) 
       {
          cerr << "File could not be opened, sorry" << endl;
          return 1;
       }
    
       while( fin >> text )   // Put the textfile in the array
          cout << text;
    
       fin.close();   // Close the file (we dont need it)
    
       for( int i = 0; i < strlen(text); i++ )
       {
          switch( text[i] )
          {
             case 'a':
                // do whatever
                break;
             default:
                // do whatever
                break;
          }
       }
    
       ofstream fout("whatever2.txt");  // Open outfile
    
       if( !fout ) 
       {
          cerr << "File could not be opened, sorry" << endl;
          return 1;
       }
    
       fout << text;  // Write to file
    
       return 0;   // ofstream's destructor closes the file
    }
    Good luck. I think your encryption idea is kind of impractical though. Go on google and look at Casaerian Encryption (aka rot13) and try and make a program around that. It is much more practical. And then after that, look at XOR encrypting
    Last edited by biosx; 03-19-2002 at 03:59 PM.

  15. #15
    Registered User
    Join Date
    Feb 2002
    Posts
    591
    Something like

    Code:
    switch(useme[i]) 
    { 
        case 'a': 
        {
            useme[i] = ''; 
            break;
        } 
    }
    I think your file problem is because you don't close the infile before you open the out file and then you don't close the out file at all.
    Last edited by Barjor; 03-19-2002 at 03:58 PM.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 01:04 PM
  2. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 04:02 AM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 01:53 AM
  4. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21