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

This is a discussion on question: i/o data form .txt or .xls files ?? within the C++ Programming forums, part of the General Programming Boards category; What did I tell you about code tags?...

  1. #16
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    What did I tell you about code tags?
    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

  2. #17
    Registered User
    Join Date
    Nov 2007
    Posts
    8
    Quote Originally Posted by CornedBee View Post
    What did I tell you about code tags?
    i'm so so sorry i made an edit

  3. #18
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,045
    Code:
    while (!data.eof())
    Bad idea. http://faq.cprogramming.com/cgi-bin/...&id=1043284351

    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
    Well, here's what your code does right now.
    • Read lines until the end of the input file.
    • Write the last line read to the output file.

    In effect, it writes the last line of the input file to the output file.

    What you need to do is to read integers, not lines. Since you know the format of the input file, you could probably
    • read the integer a from the input file;
    • read the integer b from the input file;
    • calculate the sum of these two numbers, storing the result in c;
    • write a, b, and c to the output file.

    Does that give you any ideas?
    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.

  4. #19
    0x01
    Join Date
    Sep 2001
    Posts
    88
    Code:
    // Assuming only 26 numbers on 1 line
    #include <fstream>
    #include <iostream>
    #include <string>
    #include <sstream>
    
    using namespace std;
    
    int main(void)
    {
    	ifstream input("in.txt");
    	ofstream output("out.txt");
    
    	string     line;
    	stringstream ss;
    	double num;
    	double sum;
    	char   idx;
    
    	while ( getline(input, line) )
    	{
    		ss << line;
    		sum = 0;
    		idx = 'a';
    
    		while (ss >> num)
    		{
    			output << idx++ << '=' << num << ends;
    			sum += num;
    		}
    
    		output << idx << '=' << sum << endl;
    		ss.clear();
    	}
    
    	input.close();
    	output.close();
    
    	return 0;
    }
    in.txt
    Code:
    3 5 7 6 9
    4 5 6 3 2 1 3 43 66 34 12 76 34
    1 2 3
    out.txt
    Code:
    a=3 b=5 c=7 d=6 e=9 f=30
    a=4 b=5 c=6 d=3 e=2 f=1 g=3 h=43 i=66 j=34 k=12 l=76 m=34 n=289
    a=1 b=2 c=3 d=6
    Last edited by knave; 11-15-2007 at 11:41 AM. Reason: dwks's comments (I agree)

  5. #20
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,045
    Yes, well, that's one way to do it. However, your variable names are pretty terse -- it's generally best to use longer names when you're posting code for others to learn from. And really, that's not the easiest code for someone to learn from; it's too complicated. You have to figure that someone who can't read two numbers, add them, and print them isn't going to learn much from something like that.

    In addition, if you're going to all of the trouble to use a vector, don't hard-code the size of the vector into the program. That's one nice thing about vectors -- they can expand to hold new data as you see fit.

    [edit] On the other hand, you don't need to remember previous numbers at all. See below. [/edit]

    Really, it's quite simple. As long as you know there are two numbers:
    Code:
    int a, b, c;
    input >> a >> b;
    c = a + b;
    output << a << ' ' << b << ' ' << c << '\n';
    If you want to sum some arbitrary number of numbers, it's easier to keep a running total. With only one line, something like this would work.
    Code:
    int var, sum = 0;
    while(input >> var) {
        output << var << ' ';
        sum += var;
    }
    
    output << sum << '\n';
    With multiple lines, it might be necessary to read each line separately and parse that string with stringstreams as knave has shown.
    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.

  6. #21
    Registered User
    Join Date
    Nov 2007
    Posts
    8
    Thank you Knave for your code, its exactly do what I need...

    You have to figure that someone who can't read two numbers, add them, and print them isn't going to learn much from something like that.
    you're right dwks i didn't understand a lot from the code and there are a lot of new codes that I've to understand before i used it...

    cheers

  7. #22
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,045
    you're right dwks i didn't understand a lot from the code and there are a lot of new codes that I've to understand before i used it...
    Sorry if I sounded insulting, but knave's code really was pretty complicated before [s]he edited it.

    @knave: One last thing: you do know that the std::ends manipulator function prints a NULL, right?
    Code:
    output << idx++ << '=' << num << ends;
    I would think a simple space would be better . . .

    Oh, and how did you syntax highlight that code?
    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.

Page 2 of 2 FirstFirst 12
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, 05: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, 12:45 PM

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