GIDForums  

Go Back   GIDForums > Computer Programming Forums > CPP / C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 09-Jun-2007, 14:30
cvirus cvirus is offline
New Member
 
Join Date: Jun 2007
Posts: 7
cvirus is on a distinguished road
Question

Read from files


CPP / C++ / C Code:
#include <cstdlib>
#include <iostream>
#include <fstream>

using namespace std;

int n;

void input_output()
{   
    ifstream input("c:\\file_in.txt");
    ofstream out("c:\\file_out.txt");
    while (input>>n)
    {
input>>n;
    out<<"\n"<<n<<endl;
    }
    input.close();  
}

int main()
{
input_output(); 
      system("PAUSE");
}

Hello, i´ve done this code and i ve create in my c drive the input file, what i want to know is : how can i pic the numbers in the input file and then make the calculations and send to the output file.

Thank you
  #2  
Old 09-Jun-2007, 19:09
dlp dlp is offline
Junior Member
 
Join Date: May 2006
Posts: 88
dlp will become famous soon enough

Re: Read from files


We need more information about what exactly you are trying to do, but a starter...

CPP / C++ / C Code:
while( input>>n )

This already reads the first number. By calling:
CPP / C++ / C Code:
input>>n;

You're reading the next number and ignoring the first.

By reading the number into n, you can do anything you want to do with them. Add it, subtract it, multiply it, divide it (though I don't suggest it with an int). It depends what you want to do your number read (n). When you're done with it, your loop automatically reads the next number.
  #3  
Old 10-Jun-2007, 12:28
cvirus cvirus is offline
New Member
 
Join Date: Jun 2007
Posts: 7
cvirus is on a distinguished road

Re: Read from files


CPP / C++ / C Code:
    ifstream input("c:\\file_in.txt");

I want go to this .txt file and put the numbers that exit in it to a stack or array and then make calculations and send them to the output file.
  #4  
Old 10-Jun-2007, 15:22
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,627
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Read from files


Quote:
Originally Posted by cvirus
I want go to this .txt file and put the numbers that exit in it to a stack or array and then make calculations and send them to the output file.

1. Read from input file: Your program does this, but, as dlp tried to point out, the program skips every other input value from the file.

2. Put the input numbers into an array: Declare an array and put each input value into the array. Start with array index = 0 and increment the index for every input value that you read.

3. Make calculations: OK. What array? What calculations? It's your program---do whatever you want.

4. Write results to output file. Your program writes to output file. I would suggest that for learning and debug purposes you might want to print results to the standard output also (cout <<)

For example, here I simply read each value, perform a calculation, and put the result into an output file. If you want to store the values in an array, you can use count as the array index.
CPP / C++ / C Code:
#include <iostream>
#include <fstream>

using namespace std;

int main()
{

    char *inname  = "test.txt";
    char *outname = "test.out";

    int invalue;
    int result;

    ifstream instream(inname);
    if (!instream) {
        cout << "Couldn't open " << inname << " for reading." << endl;
        return 0;
    }
    ofstream outstream(outname);
    if (!outstream) {
        cout << "Couldn't open " << outname << " for writing." << endl;
        return 0;
    }

    int count = 0;
    while (instream >> invalue) {
        ++count;
        cout << "Item number " << count
             << ": value = " << invalue << endl;
        result = invalue * invalue - 1;
        outstream << result << endl;
        if (!outstream) {
            cout << "There was a problem writing item number "
                 << count << " to " << outname << endl;
            return 0;
        }
    }
    instream.close();
    outstream.close();
    cout << "The total number of items = " << count << endl;
}

Input file "test.txt":
Code:
1 2 3 4 5 99 98 100 -12334465

Result of program run (console output):
Code:
Item number 1: value = 1 Item number 2: value = 2 Item number 3: value = 3 Item number 4: value = 4 Item number 5: value = 5 Item number 6: value = 99 Item number 7: value = 98 Item number 8: value = 100 Item number 9: value = -12334465 The total number of items = 9

Output file "test.out" after the run:
Code:
0 3 8 15 24 9800 9603 9999 -1599689984

Regards,

Dave
  #5  
Old 11-Jun-2007, 09:47
cvirus cvirus is offline
New Member
 
Join Date: Jun 2007
Posts: 7
cvirus is on a distinguished road

Re: Read from files


CPP / C++ / C Code:
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <iomanip>

using namespace std;

int n;

void input_output()
{   
    int count=0;
    ifstream input("c:\\file_in.txt");
    ofstream out("c:\\file_out.txt");
    while (input>>n)
    {
          count++;
    out<<"\n"<<n<<endl;
    cout<<"valor n"<<count<<": "<<n<<endl;
    }
    
    input.close();
    out.close();  
}

int main()
{
input_output(); 
      system("PAUSE");
}

Thank you davekw7x great explanation, but now i need to put this values into
a a stack or into an array, how do i do this operation?
  #6  
Old 11-Jun-2007, 09:52
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,627
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Read from files


Quote:
Originally Posted by cvirus
now i need to put this values into
a a stack or into an array, how do i do this operation?

As I said before:

Quote:
Originally Posted by davekw7x
2. Put the input numbers into an array: Declare an array and put each input value into the array. Start with array index = 0 and increment the index for every input value that you read.
Regards,

Dave
  #7  
Old 11-Jun-2007, 12:44
cvirus cvirus is offline
New Member
 
Join Date: Jun 2007
Posts: 7
cvirus is on a distinguished road

Re: Read from files


I have read what you post, but i don´t know how to put those numbers into an array.

Thank you
  #8  
Old 11-Jun-2007, 12:55
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,627
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Read from files


Quote:
Originally Posted by cvirus
but i don´t know how to put those numbers into an array.
Look for examples in your book or class notes.

This is how you declare an array:

CPP / C++ / C Code:
   int x[100]; /* an array of 100 ints */

This is how you store something in the array (note that the value of count must be greater than or equal to zero and less than or equal to 99 for the above declaration)

CPP / C++ / C Code:

int something;
int count;
.
.
.
.
    x[count] = something;
.
.
.

Regards,

Dave
  #9  
Old 14-Jun-2007, 11:24
cvirus cvirus is offline
New Member
 
Join Date: Jun 2007
Posts: 7
cvirus is on a distinguished road

Re: Read from files


CPP / C++ / C Code:
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <iomanip>

using namespace std;

int n;

void input_output()
{   
    int count=0;
    int v[100];
    ifstream input("c:\\file_in.txt");
    ofstream out("c:\\file_out.txt");
    while (input>>n)
    {
          count++; 
    out<<"\n"<<n<<endl;
    cout<<"valor n"<<count<<": "<<n<<endl;
    v[count]=n;
    }
    input.close();
    out.close();  
}

int main()
{
input_output(); 
      system("PAUSE");
}

I made this way, but it´s not working. Is this you mean?
  #10  
Old 14-Jun-2007, 12:12
dlp dlp is offline
Junior Member
 
Join Date: May 2006
Posts: 88
dlp will become famous soon enough

Re: Read from files


what about it isn't working?
 
 

Recent GIDBlog2nd Week of IA Training by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Airport Log program using 3D linked List : problem reading from file batrsau C Programming Language 11 29-Feb-2008 07:44
hOW TO READ XLS FILES IN C/c++ naveen CPP / C++ Forum 2 06-Jun-2005 13:47
Apache2 config issues monev Apache Web Server Forum 2 28-Jun-2004 06:19
Can't view pages from another machine on the Intranet aevans Apache Web Server Forum 9 14-May-2004 02:26
C++, files calculus87 CPP / C++ Forum 0 24-Sep-2003 10:01

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 03:39.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.