Stream I/O


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include<iostream>
#include<cstdlib>
#include<iomanip>
#include<fstream>
#include<string>

using namespace std;


ifstream in_stream;
ofstream out_stream;

char infilename[21], outfilename[21];

   
int main()
{

        
cout <<"Please enter the input file name including the extension (i.e. .txt,.exe)\n";
cout <<"(1 - 20 characters):";
    cin >>infilename;
cout <<"Thank you, you entered "<<infilename<<endl;

cout <<"Please enter the output file name including the extension (i.e. .txt,.exe)\n";
cout <<"(1-20 characters):";
    cin >>outfilename;
cout <<"Thank you, I will now write to " <<outfilename<<endl;
    
        

in_stream.open(infilename);      
      

    if (in_stream.fail( ))
    {
        cout << "Input file opening failed.\n";
        exit(1);
    }
    
        
     out_stream.open(outfilename);
     
     
     
     if (out_stream.fail( ))
    {
        cout << "Output file opening failed.\n";
        exit(1);
    }
     
     writesales();
     

    in_stream.close();
    out_stream.close();
    
    return 0;

} 

 







That Is the code that I have written so far, But it is still in progress. I am supposed to open a file (i will include the file info in a moment) and take the data and calculate the data and return it into an outfile. The thing that i am not sure on how to do is to read a line from the file and to calculate it, not to calculate the whole thing. So i dont know if it would be an array or what.

If anyone has any ideas on how to take one line from the file and then do the calculation and put it in the outfile i would really appreciate it.

the file that i have to read the info from looks like this :
4 3 // number of people, then weeks
lastName1 firstName1 1234.4 546.9 541.9 7654.7 876.98
lastname2 firstName2 987.6 874.9 234.7 85.5 1965.4
lastName3 firstName3 24.4 547.9 54.9 764.7 6543.5
lastname4 firstName4 97.6 874.9 234.7 865.5 345.6

lastName1 firstName1 34.4 5416.9 1541.9 154.7 816.98
lastname2 firstName2 97.6 8174.9 14.7 85.5 165.4
lastName3 firstName3 2314.4 76.9 54.9 74.7 643.5
lastname4 firstName4 974.6 844.9 234.7 825.5 145.6

lastName1 firstName1 14.4 56.9 5441.9 7454.7 76.98
lastname2 firstName2 987.6 87654.9 1234.7 8765.5 965.4
lastName3 firstName3 24.4 546.9 544.9 765.7 653.5
lastname4 firstName4 977.6 74.9 534.7 85.5 45.6

the calculations is to calculate

The number of sales persons and weeks that were processed
Total and average sales for each salesperson for each period
Total sales and average sales for each salesperson over all periods
Grand total sales and average sales for all persons for all periods


please help!
Please don't post in multiple forum sections


Here you go: http://lmgtfy.com/?q=text+file+io+c%2B%2B
Last edited on
well, i wasnt getting a response, and you dont have to be rude. I have googled it several times, the last thing i do is revert to forums. but since you want to be a dick just forget it.

people are already frustrated enough by the time they get to the point of asking for help, try actually helping instead of being a smart ass.
Rude? Those first two links are actually very helpful, they tell you everything that you need to know and everything that people will probably tell you in this post. The second link is even the tutorial on this site, which is often referenced. I was trying to speed up your homework process so you didn't have to wait for people's responses.
It was rude at the way you passed it along to me as if i was a stupid brat. A link that just sent me there would have been efficient
The point is to make you learn how to teach yourself. If your problem is that you can't figure out how to read a line from a file, I suggest the following http://lmgtfy.com/?q=c%2B%2B+file+readline. If you can't figure out how to parse the line, that is a different matter and requires a bit of skill. But a polite monkey could figure out how to read a line.

The reason why Tevsky was "rude" as you claim, is to get you to try and figure out how to answer your own questions. If every single person who was in a class being taught how to read a line from a file came and started a thread on the topic, the forum would be filled with nonsense.

It was rude at the way you passed it along to me as if i was a stupid brat. A link that just sent me there would have been efficient
In the short run yes, in the long run no. Also you do not have a right to our time. Your first sentence indicates that you believe that you do, but you do not. We are willing to help you, but not if it is something like this.

people are already frustrated enough by the time they get to the point of asking for help, try actually helping instead of being a smart ass.
he was helping you, perhaps not in the way you wanted.

@tevsky, thanks for the site, it will be very handy.

Well yes, I know i understand that I shouldnt be fed the answer, but the way he sent me to the google link was rude.

I dont deserve anyones time, i have to earn it. I am appreciative of anyhelp i get from the people on these forums but I guess you could just say that I am so frustrated that i cant figure out how to read a line, then calculate it.

And since im not a "polite money" who can figure it out, Thanks for the links and I will try to find the answers within those results.

I apologize for being an ass, I am just stressed out about this.

Have a great day

I personally think that the lmgtfy stuff is a bit rude... And I would be disappointed if it begins to crop up more than usual. Google only gives you great hits when you know exactly what to ask for...

C++ I/O takes a little getting used to. And thinking in the proper terms make or break your google searches.
@Duoas, the problem is that lustforpain didn't post any code related to attempting file in/out, and while your point about the proper terms is valid, reading from a file in c++ is a fairly basic topic. That said, if his question was about how to parse strings to separate the names from the values he wanted to use, that would be a different story.
Isn't reading a number from a file as easy as reading a word?
I usually read a line, then extract the formatting from there. You have to options, fscanf and getline(cin, astring). From astring convert to a stringstream and then extract the two names and the doubles. The problem above, is that lust was not even demonstrating that he can read anything from a file, a fairly basic topic.
While the documentation on this site is, overall, excellent, I did think the file i/o tutorial was a little bit less so.

If you want to extract numbers from a file, you could do this:
1
2
3
4
5
6
7
8
9
10
11
// Open the text file, take the first line from it, and store it to stringToExtractNumbersFrom
string stringToExtractNumbersFrom; // Shorten the name of this...
ifstream file;
file.open("filename.txt");
getline(file, stringToExtractNumbersFrom)
file.close();
for (int i = 0; i < 9; i++) {
    while (stringToExtractNumbersFrom.find(i) != string::npos) {
        // Code to take numbers from the string -- you should be able to figure this out yourself...
    }
}


Do people here forget about the >> operator overloads?

1
2
3
ifstream file ( "this file contains only numbers separated by spaces.txt");
double firstNumberInTheFile;
file >> firstNumberInTheFile;
We don't, I just prefer to use getline.

@chrisname, you code won't work for a case like 10, it will first try and extract the 0, then the 1.

I prefer:

1
2
3
4
5
6
7
for(int i=0;i<myString.size();++i)
{
   if(('0'<=myString[i] and myString[i]<='9') or '.'==myString[i])
   {
       //code to extract numbers, and handle for loop iteration
   }
}
Last edited on
why do you convert a line of the file stream into a string, then into a stringstream and read from them if reading from a stringstream is the same as reading from a file stream? KISS
I've gotten bugs using ifile>>aDouble.

Also I usually do the following

DoubleArray[x]=atod(aString.substr(beginning of double, charLengthOfDouble).c_str());
atod can never be better than >> and I think that isn't even a standard function
strtod or atof, it is in cstdlib
Topic archived. No new replies allowed.