Thread: String from file to ASCII

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    7

    Unhappy String from file to ASCII

    Hey,
    Im having trouble converting each character from a string in a file to ASCII. I tried to use inFile and put each character into a variable, then convert the variables using static_cast<int>, but I keep getting a huge number. Any pointers would be appreciated. Thanx.

  2. #2
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Code please.

  3. #3
    Registered User
    Join Date
    Mar 2006
    Posts
    7
    ok, heres the code;

    Code:
    #include <iostream>
    #include <fstream>
    #include <cstring>
    
    using namespace std;
    int main()
    {
        int r1;
        char num1, num2, num3, num4, num5, num6, num7, num8, num9, num10;
        char num11, num12, num13, num14, num15, num16, num17, num18, num19, num20;
        //declare other variables here
        
        ifstream inFile; //declare the input file
        char filename[20];//filename is used to store the name of the input file
        
          cout << "Enter the name of the input file: "<<flush; //enter the name of the input file
        cin >> filename; //read the name of the input file
        inFile.open(filename); //open the input file 
        
        
        // ************************************************************ //    
        //                   Write your code here                       //
        
        inFile >>num1>>num2>>num3>>num4>>num5>>num6>>num7>>num8>>num9>>num10 
               >>num11>>num12>>num13>>num14>>num15>>num16>>num17>>num18>>num19>>num20;
        
        r1 = static_cast<int>(num1+num2+num3+num4+num5+num6+num7+num8+num9+
          num10+    num11+num12+num13+num14+num15+num16+num17+num18+num19+num20);
        
        cout<<r1<<endl;
       cout<<num1<<num2<<num3<<num4<<num5<<num6<<num7<<num8<<num9<<num10<<
       num11<<num12<<num13<<num14<<num15<<num16<<num17<<num18<<num19<<num20<<endl;
    
      
        
            inFile.close(); //close the input file 
    
        cout << endl;                                 
    cout << "Press CTR-C to Leave..." << endl;    
    pause();                                      
    return 0;                                                                                                       
    }
    The files contain strings such as:

    How are you?

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    It would be much easier just to do something like, read in the strings, shove them in a string variable. and use the copy algorithm. An easy ostream_iterator<int>(fstream, " "); can output the ASCII code to another file.

  5. #5
    Registered User
    Join Date
    Mar 2006
    Posts
    7
    I didnt really want to move the information to another file, because I need it to do more calculations. This bit is really bugging me, and its the first step in the prgram, everthing else Ive been able to do, but using numbers iv inputted.

  6. #6
    Registered User
    Join Date
    Feb 2006
    Posts
    312
    I'm not sure I follow - The file contains text/strings? or numerical data?

    You can retrieve strings from a file in exactly the same way you retrieve them from the user with cin.

    I recommend you get rid of <cstring> and use the C++ std::string instead from the <string> header. Then you can retrieve a line of input from your ifstream with the following:
    Code:
    std::ifstream ifs("test.txt");
    std::string myString;
    std::getline(ifs, myString);
    Last edited by Bench82; 04-28-2006 at 04:00 PM.

  7. #7
    Registered User
    Join Date
    Feb 2006
    Posts
    312
    Apologies.. I see now - You're accumulating the sum of the numerical ASCII codes... maybe something like this?
    Code:
    std::ifstream ifs("test.txt");
    int sum(0);
    while(ifs)
        sum += static_cast<int>(ifs.get());
    Last edited by Bench82; 04-28-2006 at 04:00 PM.

  8. #8
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Quote Originally Posted by Bench82
    I'm not sure I follow - The file contains text/strings? or numerical data?

    You can retrieve strings from a file in exactly the same way you retrieve them from the user with cin.

    I recommend you get rid of <cstring> and use the C++ std::string instead from the <string> header. Then you can retrieve a line of input from your ifstream with the following:
    Code:
    std::ifstream ifs("test.txt");
    std::string myString;
    std::getline(ifs, myString);
    That plus:
    Code:
    #include <numeric>
    
    ...
    
    int sum = std::accumulate( myString.begin(), myString.end(), 0 );
    Is all you need to get the sum of the value of all the characters in a string.



    Quote Originally Posted by Bench82
    Code:
    while(ifs)
        sum += static_cast<int>(ifs.get());
    That won't quite work, it runs one extra time through the loop (returning -1) because the stream doesn't get set to eof until after you read past the end of the file.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  9. #9
    Registered User
    Join Date
    Mar 2006
    Posts
    7
    hmmm..that would be all fine and dandy, except I cant change the header files. I tried to use static_cast<int> to change from char to ASCII, but the compiler complains. I think it has something to do with the char[20]. I think I have to do something with arrays, but I havent learnt them yet. I think Im about to resign to the fact that I have failed this task miserably. I know its so simple though! agh!driving me nuts!

  10. #10
    Registered User
    Join Date
    Mar 2006
    Posts
    7
    im gonna try

    Code:
    while(ifs != inFile.eos())
          sum+=static_cast<int>(ifs.get());
    maybe that will stop it running through too many times?!

  11. #11
    Registered User
    Join Date
    Feb 2006
    Posts
    312
    Quote Originally Posted by hk_mp5kpdw
    That won't quite work, it runs one extra time through the loop (returning -1) because the stream doesn't get set to eof until after you read past the end of the file.
    Good point, I didn't think about that.
    Quote Originally Posted by kilastria
    maybe that will stop it running through too many times?!
    Unfortunately not. The problem as hk_ pointed out, is that ifs.get() occurs after the while condition is evaluated (in both cases, the stream is in a good state until after ifs.get() is called at the point of EOF - causing -1 to be added). a better solution might be to put the ifs.get() inside the while brackets.
    Code:
    ifstream ifs("test.txt");
    int sum(0);
    char c;
    while(ifs.get(c))
        sum += static_cast<int>(c);
    This stops if the stream is empty, hence keeps the -1 from being added to sum
    Last edited by Bench82; 04-29-2006 at 05:46 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  2. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  3. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  4. Headers that use each other
    By nickname_changed in forum C++ Programming
    Replies: 7
    Last Post: 10-03-2003, 04:25 AM
  5. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 11:06 PM