Thread: question: i/o data form .txt or .xls files ??

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    8

    Question question: i/o data form .txt or .xls files ??

    Hi everyone,

    I have just start working at my final year project in communication engineering and I’m doing some c++ programming as well. My question is how can I read numbers from a .txt or .xls file and make some calculation with it, then output this calculation in another .xls file.

    Lets say the file that I want to read from contains: "4 5"

    and i would like to create another .xls file that contains: "4 5 9" (9 is the sum of the two numbers)

    please everyone I need your help ASAP

    cheers

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    http://wotsit.org/

    Or search for XLS reading libraries. Note that Microsoft changes their formats very often, so supporting them all is extremely difficult.

    You'd be much better off simply supporting comma-separated value files, CSVs. Excel can import and export them, as can every other program working with tables.

    As for text I/O, that should be simple enough.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Stick to text files, it will make your life SO much easier.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Nov 2007
    Posts
    8
    "As for text I/O, that should be simple enough."

    "Stick to text files, it will make your life SO much easier."

    thanks for your help CornedBee and Salem, i think i will do my work in txt files to avoid any problems, BUT i still didn't know what to write?? what is the source code??

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Do you know how to read text files in general?

    For CSV files, you read the text file line by line and split each line into the fields. You can use the Boost.Tokenizer library there.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Ok, so show us where you are stuck? Generally, we don't write code for beginners - it's much better that you do it yourself, and when you don't know what to do, ask here [with a SMALL example of your code - not hundreds of lines]. It also helps if you are precise about what you are experiencing difficulty with - e.g. if you are trying to add two numbers together, what numbers are you using, and what result do you get [and if it's not absolutely obvious, what do you EXPECT it to be].


    There are plenty of tutorials on "reading data from a file" on this site and in other places.

    Go to www.cprogramming.com for tutorials.

    Doing math on the data depends a little bit on what you actually want to do, and what sort of data you have, and so on.

    Do you have a book on C++ - if not, there's a "book recommendations" thread that you should be able to find a few suggestions in.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    Nov 2007
    Posts
    8
    ok to save everyone's time this is what i wrote earlier:

    ********************************************
    #include <iostream>
    #include <fstream>
    using namespace std;

    int main()
    {

    char str[20];
    fstream file_op("test.txt"); // "test.txt" contains two numbers 3 and 5 separated by space
    while(file_op >> str)
    cout << str ;
    file_op.close();

    return 0;
    }

    ***************************************

    so from this source code it will read the two numbers from the txt file and output them ....

    BUT instead of that i would like to output the two numbers in another txt file and inside it i want this to be shown:
    a=3 b=5 c=8

    thats all .....??

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Please use [code][/code] tags to post code. It preserves the formatting.

    Inside what do you want what to be shown? Where do a, b and especially c come from?
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  9. #9
    Registered User
    Join Date
    Nov 2007
    Posts
    8
    Quote Originally Posted by CornedBee View Post
    Please use [code][/code] tags to post code. It preserves the formatting.

    Inside what do you want what to be shown? Where do a, b and especially c come from?
    first, i would thank you CornedBee for your question and your patient with me. second, i really can't explain more about what i want so if you tell me what you have understand from what i wrote and i'll make some edit on what you'll say.

    cheers

  10. #10
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Well, the first thing is, do you know how to write to another text file as well?

    BTW, you probably want to read data into an integer, not a string.

    So, you have a file like
    Code:
    3 5 8
    and you want to generate another file like
    Code:
    a=3 b=5 c=8
    Is that right?
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  11. #11
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> second, i really can't explain more about what i want so if you tell me what you have understand from what i wrote and i'll make some edit on what you'll say.

    do you really expect to be spoon fed the answer? a little more effort, please.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  12. #12
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    You want to do two things:

    1) "i would like to output the two numbers in another txt file"

    2) "inside it i want this to be shown:
    a=3 b=5 c=8"

    Where do these three numbers come from, and why are they called a, b and c? Do you want the output from #2 to be inside the text file as well?
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  13. #13
    Registered User
    Join Date
    Nov 2007
    Posts
    8
    Quote Originally Posted by dwks View Post
    Well, the first thing is, do you know how to write to another text file as well?

    BTW, you probably want to read data into an integer, not a string.

    So, you have a file like
    Code:
    3 5 8
    and you want to generate another file like
    Code:
    a=3 b=5 c=8
    Is that right?
    yes, i now how to write to another text file.
    yes, i have a txt file like: 3 5
    no, i want to generate another txt file like: a=3 b=5 c=8 \\ where c=a+b=8


    Quote Originally Posted by CornedBee View Post
    You want to do two things:

    1) "i would like to output the two numbers in another txt file"

    2) "inside it i want this to be shown:
    a=3 b=5 c=8"

    Where do these three numbers come from, and why are they called a, b and c? Do you want the output from #2 to be inside the text file as well?
    i want to do three things:
    1) read in integers from a txt file \\ which are: 3 5
    2) import this integers and defined 3 as "a" and 5 as "b" then add 3 and 5 together to present c
    3)then generate #2 to another txt file.

    thats all...

  14. #14
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    care to post the code you have thus far?
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  15. #15
    Registered User
    Join Date
    Nov 2007
    Posts
    8
    Quote Originally Posted by Sebastiani View Post
    care to post the code you have thus far?
    this is the code:
    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    using namespace std;
    
    int main ()
    {
    	string line;
    	ifstream data;
    	data.open ("input.txt");
    	while (!data.eof())
    	{
    	getline(data, line);
    	}
    	
    	ofstream result;
    	result.open ("output.txt");
    	result << line;
    	result.close ();
    	
    	data.close ();
    	
    	return 0;
    }
    **************************************

    in this code i import and export "line" from the files which is not what i'm looking for, i want to import and export individual integers from the files
    Last edited by camelman; 11-14-2007 at 03:17 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. question regarding outputting data to adobe flash
    By hoax464 in forum C++ Programming
    Replies: 1
    Last Post: 07-21-2008, 01:08 PM
  2. Question about binary trees and files
    By satory in forum C Programming
    Replies: 9
    Last Post: 03-06-2006, 06:28 AM
  3. Reading data from consecutively named files
    By a1pro in forum C Programming
    Replies: 10
    Last Post: 04-15-2005, 01:48 AM
  4. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM
  5. Write and use data stored in data files...
    By alex6852 in forum C++ Programming
    Replies: 2
    Last Post: 11-01-2001, 01:45 PM