Thread: Read string convert and write

  1. #1
    Registered User
    Join Date
    Jan 2020
    Posts
    12

    Read string convert and write

    Hello,


    I have a file named "file_in.txt".
    This file has a list of real numbers - one number in evey line. For example:


    Code:
    231
    -3442.14
    12
    -431.244
    0
    14335
    0.14
    -0,15
    .
    .
    .
    I wrote a function named : "float_to_fixed" that can accept a floating point number and convert it to fixed point ( represented by an integer number ).
    I want to read "file_in.txt", use my function and write the converted value to a file named "file_out.txt" ( also value in a line ).


    Code:
    #include <iostream>
    #include <fstream>
    using namespace std ;
    
    
    int main ( )
    {
        ifstream file_in ( "file_in.txt", ios::in | ios::app | ios::binary ) ;
        ofstream file_out ( "file_out.txt", ios::out | ios::app | ios::binary ) ;
     
        // maybe use these functions together with my "float_to_fixed" ?
        // std::stof();
        // std::to_string()    
        return 0 ;
    }
    Please help me complete the code.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    This doesn't sound like a good idea: you might lose accuracy reading the input as a float to begin with. Why not read the input as a string then write and apply a string_to_fixed function?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jan 2020
    Posts
    12
    Thanks for your input. But I don't quite understand what you suggest to do.

    My function works on a floating point type numbers. So one way or another - I must convert the strings in the file (which represent floating point numbers) to type floats.
    Why should I do this on my own ? Do you expect my custom string to float conversion to work better than stof() ?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Oh, then where does fixed point come into play?

    EDIT:
    Oh, okay, I think I understand: 'write the converted value to a file named "file_out.txt"'. In that case, there's nothing more to do. Just write the integer as output. As simple as that. While you could convert the integer to string first, that would be unnecessary.
    Last edited by laserlight; 06-02-2020 at 05:47 PM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Jan 2020
    Posts
    12
    Sorry if I wasn't clear enough at first. I'll explain again:
    Someone sends me a text file that contains strings representing floating point values (as shown in the first post).
    I need to convert these values to actual floating point numbers, apply my function that returns integer, convert these integers to strings and write them to a second file.
    This second file will be used by an entirely different software that expects a file in that format...

    So what do you suggest ?

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by bitslip
    My function works on a floating point type numbers. So one way or another - I must convert the strings in the file (which represent floating point numbers) to type floats.
    I thought that you were dealing with fixed point numbers, and were reading as float and then converting to fixed point for calculations.

    Quote Originally Posted by bitslip
    Why should I do this on my own ?
    Because if you're working with fixed point, then going through float first might introduce inaccuracy.

    Quote Originally Posted by bitslip
    Do you expect my custom string to float conversion to work better than stof() ?
    No, I expect your custom string to fixed point function to work better than stof followed by your float_to_fixed function if you were indeed dealing with fixed point rather than float.

    However, if you aren't doing any floating point calculations, i.e., if the entire purpose of the program is to convert from one format to another, then the idea still applies: bypass float by reading as string, then convert directly to fixed point.

    Quote Originally Posted by bitslip
    Someone sends me a text file that contains strings representing floating point values (as shown in the first post).
    I need to convert these values to actual floating point numbers, apply my function that returns integer, convert these integers to strings and write them to a second file.
    This second file will be used by an entirely different software that expects a file in that format...
    I suggest that instead of "convert these integers to strings and write them to a second file", you write the integers to a second file, letting the ostream functionality do the conversion for you. For example:
    Code:
    int num = 123;
    file_out << num;
    Last edited by laserlight; 06-02-2020 at 05:57 PM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Dec 2017
    Posts
    1,631
    Your input file is text, not binary.
    If you need your output to be binary, what exactly is the format?
    I've output to a text file below, one integer per line.
    Never say ios::in for an ifstream or ios::out for an ofstream (that would be silly if you had to do that!).
    ios::app doesn't make sense for an input-only file.
    You may not want ios::app on your output file, either.
    Code:
    #include <iostream>
    using namespace std;
     
    int main()
    {
        ifstream in("file_in.txt");
        ofstream out("file_out.txt");
        double x;
        while (in >> x)
            out << long(x * 100) << '\n'; // or however you convert them
    }
    A little inaccuracy saves tons of explanation. - H.H. Munro

  8. #8
    Registered User
    Join Date
    Jan 2020
    Posts
    12
    Thanks,

    So...I have the my function that's called : "float_to_fixed".


    Code:
    fixed_point_int = float_to_fixed ( converted_string_to_float )
    
    // "converted_string_to_float" is the float from the original file. 
    // "fixed_point_int" is the value that I needs to be written to the second file as a string
    Can you help me complete the code ?

    Code:
    #include <iostream>
    #include <fstream>
    using namespace std ;
    
    
    
    
    int main ( )
    {
        ifstream file_in ( "file_in.txt", ios::in | ios::app | ios::binary ) ;
        ofstream file_out ( "file_out.txt", ios::out | ios::app | ios::binary ) ;
      // read string as float, apply function , write int as string   
    }

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by bitslip
    Can you help me complete the code ?
    It really isn't so hard:
    Code:
    // read string as float, apply function , write int as string
    float x;
    while (file_in >> x)
    {
        file_out << float_to_fixed(x) << '\n';
    }
    But you didn't even bother to address john.c's points about ios::in etc.

    Frankly, the above code might not work with your sample input file (I noticed a line of text that may be problematic), but I can't be bothered to fix it for you until you've shown that you're actually going to try out what people suggest.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User
    Join Date
    Jan 2020
    Posts
    12
    Oh, sorry.
    I missed John's reply.
    I'll try that!

  11. #11
    Registered User
    Join Date
    Jan 2020
    Posts
    12
    So I completed the code and it seems to work fine - at least with my very basic testing.
    I'd love to get some feedback on it.

    Please comment in this thread:
    double_to_fixed function and file IO

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 10-28-2015, 02:07 PM
  2. How to convert/write data to XML file?
    By HiepDT in forum C Programming
    Replies: 4
    Last Post: 10-07-2011, 04:31 AM
  3. Convert function to write ascii instead of binary
    By ofey in forum C Programming
    Replies: 3
    Last Post: 07-16-2010, 04:03 PM
  4. Read command line and write to file or string
    By miiisuuu in forum C Programming
    Replies: 1
    Last Post: 10-21-2007, 11:39 AM
  5. How to convert char read from file into string
    By cruxxe in forum C Programming
    Replies: 7
    Last Post: 05-22-2002, 02:09 PM

Tags for this Thread