I am lost on how to read from file and output to file?

This is a discussion on I am lost on how to read from file and output to file? within the C++ Programming forums, part of the General Programming Boards category; Code: I am not sure how to read from file. For example, i have a file said 2 1 2 ...

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    1

    I am lost on how to read from file and output to file?

    Code:
    I am not sure how to read from file.  For example, i have a file said
    2
    1 2
    3
    1 2 3
    
    if i need to read the first number represent how many number and 2nd line show the number 1 and 2
    when i read 3rd line it show there are 3 number so the 4th row show 1 2 3 is the 3 number how can i do it?
    when i program it i program something like this but it doesn't work?  Instead it only show
    2 number of interger
    1 number of interger 2
    3 number of interger
    1 number of interger 2 3
    
    how can i do it so that it will read first line and 3rd line (2 and 3) as an variable of number_of_interger.  And read 2nd and 4th line (1, 1, 2, and 3) as number?
    Code:
    [/PHP]
    #include <fstream>
    #include <conio.h>
    #include <iostream>
    #include <iomanip>

    #define in_file "input.txt"
    #define out_file "output.txt"
    using namespace std;
    void main()
    {
    ifstream ins;
    ofstream outs;

    //string scores;
    char number_of_interger, number;

    ins.open(in_file);
    outs.open(out_file);

    while (!ins.eof())
    {
    ins >> number_of_interger;
    cout << number_of_interger << "number"
    << " of interger";
    ins.get(card_scores);

    while (number != '\n')
    {
    outs << number;
    cout << number;
    ins.get(number);
    }
    cout << number;
    outs << number;
    }
    outs << number;
    ins >> number_of_interger;
    getch();
    ins.close();
    outs.close();
    }

  2. #2
    Registered User
    Join Date
    Aug 2003
    Posts
    1,198
    use codetags!
    use int main(), not void main()!

  3. #3
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,801
    Welcome to the boards. If you haven't already done so then please take some time to familiarise yourself with the faq:
    http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

    You might also want to read the posting guidelines:
    http://cboard.cprogramming.com/annou...ouncementid=51

    Remember, too, that the board has a search facility, a link is at the top of your screen. It will often get you a quicker answer to your questions than waiting for a response to one you have posted.


    If you have any questions about this you may ask or you can contact one of our forum leaders:

    http://cboard.cprogramming.com/showgroups.php
    Woop?

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Try this.
    Code:
    #include <fstream>
    #include <conio.h>
    #include <iostream>
    #include <iomanip>
    #include <string>
    using namespace std;
    
    const string in_file( "input.txt" );
    const string out_file( "output.txt" );
    int main()
    {
       ifstream ins;
       ofstream outs;
    
       //string scores;
       int number_of_integer, number;
    
       ins.open(in_file.c_str());
       outs.open(out_file.c_str());
    
       while (ins >> number_of_integer)
       {
          cout << number_of_integer << " number"
          << " of integer" << endl;
    
          for (int i=0; i<number_of_integer; ++i)
          {
             ins >> number;
             outs << number << " ";
             cout << number << " ";
          }
          cout << endl;
       }
       cin.get();
       ins.close();
       outs.close();
    }

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    I am lost on how to read from file and output to file?
    Since that is the only part of your post that I can understand:
    Code:
    #include<fstream>
    #include<iostream>
    #include<string>
    
    using namespace std;
    
    int main()
    {
    	ofstream outFile("C:\\Beginning C++\\data.txt");
    	outFile<<"1 "<<2<<" "<<3<<"\n"<<"4 5";
    	outFile.close();
    
    	ifstream inFile("C:\\Beginning C++\\data.txt");
    	if(!inFile)
    	{
    		cout<<"no such file"<<endl;
    		return 1;
    	}
    
    	int data[10] = {0};
    	int i = 0;
    	while(!inFile.eof())
    	{
    		inFile>>data[i];
    		i++;
    	}
    	
    	int num = sizeof data/sizeof data[0];
    	for(int k = 0; k<num; k++)
    	{
    		if(data[k] != 0)
    			cout<<data[k]<<" "<<endl;
    	}
    	
    	return 0;
    }
    You might as well write your post using your native language--English isn't working for you.
    Last edited by 7stud; 02-27-2005 at 10:56 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. I'm not THAT good am I?
    By indigo0086 in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 10-19-2006, 10:08 AM
  3. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 09:46 AM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM

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