Attempting to change data in a file

I am trying to take data within a file and perform some operations on them
So far I can read the file and also write to a different file, as in the results.
I have two files
file_to_read.txt (contains data)
file_to_write_to.txt (file that I will write results to)

I have a for loop which walks the file_to_read file

like this

for (Index = 0; Index < 20; Index++)
{
}

I'm not sure how to go about the rest
The lines 0 to 6 within the file "file_to_read" are integers.
The rest (7-20) are strings.

So is it possible that I split the entire array?
As in, one array goes from 0 to 6 the other from 6 to 20 but 6 is set as 0 in the next for loop...

I'm also not sure about how to do the mathematical operations to something which I can't see...

I'm hung up on the fact that after I read the file, and once I get to walk the for loop correctly, how do I add these "data(s)"?

I was told that I could just do string01 + string02 in the case of the strings after the loop and perhaps storing the data in memory.

Any help would be greatly appreciated

Thank you.
This just printed like a ba-jillion zeros
I reckon it has to do with the for loop not terminating


#include <fstream>
#include <iostream>
#include <string>

using namespace std;

void main (void)
{
// local variables
ifstream inFile;
ofstream outFile;
string fileName("file_to_read.txt");
string fileName2("file_to_write_to.txt");
int random;
int Index = 0;
string array_from_file[20];
int results = 0;




inFile.open(fileName);

for (Index = 0; Index < 20 ; Index++)
{
for (Index = 0; Index < 6; Index ++)
{
array_from_file[1] + array_from_file[2] + array_from_file[3] = results;
cout << results;



}


}

outFile.open(fileName2);
outFile << "I'm writing this to test to a file" ;

cout << "Prompt to see screen" << endl;
cin >> random;







}


closed account (3hM2Nwbp)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
for (Index = 0; Index < 20 ; Index++) // <- Never breaks from this loop, Index always < 20
{
    for (Index = 0; Index < 6; Index ++) // Index always reset to 0 here
    {
        
    }
}

// Should be

int outerIndex = 0;
int innerIndex = 0;

for(outerIndex = 0; outerIndex < 20; ++outerIndex)
{
    for(innerIndex = 0; innerIndex < 6; ++innerIndex)
    {
        // Do it...err why were they nested in the first place? :-\
    }
}


I think that the rest of your code needs some work as well. Can you post a sample input file?
Last edited on
Sorry
Could you elaborate more on the nested? Does that mean that one is within the other?
My class didn't really touch on nested, we had to read it on our own...

My professor was showing me a way to do it where I used two Indexes

Index1 and Index2
Here is an unfinished loop or two of them actually I just don't know what order.

Also how do you perform the operations once the data has been pulled up?

That's what I'm stuck on/can't get my head around.

for ( Index1 = 0; Index1 < 6; Index ++)
{
array_of_int[Index2}
Index2++
}

for (Index1; <20, Index1++)
{
string[Index2]
some_string[0] // Index 2 is 0 (inside the brackets)
}


I do apologize for the crappy order, I'm lost right now
closed account (3hM2Nwbp)

The lines 0 to 6 within the file "file_to_read" are integers.
The rest (7-20) are strings.


Assuming that each entry is on its own line, something like this.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
int ints[6]; //  1, 2, 3, 4, 5, 6
string strings[14]; // 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20

std::ifstream stream(fileName);

for(int i = 0; i < 6; ++i) // Fill up the integer array with lines 1 to 6
{
    stream >> ints[i];
}

for(int i = 0; i < 14; ++i) // Fill up the string array with lines 7 to 20
{
    stream >> strings[i];
}
My mistake I didn't see the last part of your post.

Here would be an input file. (at least I think this is what you mean)

int
int
int
int
int
int
word
word
word
word
.
word
word
word
word
.

I'm supposed to add, subtract, divide, multiply the integers (1-6)
Then I have to make two sentences from the words and the periods.
This is where that string01 + string02 was supposed to happen.
What is the [i] in your response? Is that just a short hand version of Index?
Also I haven't learned the use of "stream" yet, in the sense of how you used it

stream >> strings[i]

Also, could you describe in words what is going on in your latest post?

As in what does this:

stream >> strings[i]

mean?

I'm just stuck on how I add these things together from the loop.
We aren't allowed to hard code so that killed the first option in mind.

From my understanding (which could be very vague)
The array of the data will be walked by the for loop.
OK so we'd have

Index - Actual line of file
0 - 1
1 - 2
2 - 3 etc...

7 - Jane
8 - Macky
9 - is etc...

So when the loop goes through these 'cells' how do I add them together... do I need to store them first?

I mean I know it probably looks like I'm just trying to get the answer, believe me I want to learn this, however time is a problem right now as far as me entering random things found on the internet through my environment, I don't want just the straight up answer but rather a guide or something...



Anyways I appreciate your input.
I hope you can follow what I've written.
Last edited on
closed account (3hM2Nwbp)
i is just a variable. This assignment requires the use of streams. If your class hasn't covered that yet, then I don't see how the professor can expect the students to use it. It's more likely that you just aren't used to the style that I wrote it in. Does this make more sense?

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
#include <fstream>
#include <string>

using namespace std;

void main (void)
{
    // local variables
    ifstream inFile;
    string fileName("file_to_read.txt");
    int Index = 0;
    int int_array_from_file[6]
    string string_array_from_file[14];

    inFile.open(fileName);

    for (Index = 0; Index < 6 ; Index++)
    {
        inFile >> int_array_from_file[Index];
    }
    for (Index = 0; Index < 14; Index ++)
    {
        inFile >> string_array_from_file[Index];
    }
}
Yes that makes sense, that is how our Hangman and Address book programs were written.

Can I ask a dumb question, what is the purpose of the inFile >> int_array_from_file[Index]; ?
This is the storing part right?

After this point then how are the actual operations accomplished? How do I state an operation

Say I wanted to add 2 + 2
I could say
inta = 2
result = 0;

inta +inta = result;

I believe that's how I would express something literal like that...
Is the result part correct? What is the purpose of setting result = 0? (again something I picked up off the internet)

Thank you so much for the help you've provided
closed account (3hM2Nwbp)
It all depends on what you want to calculate.

1
2
// The sum of the first two integers in our array.
int result = int_array_from_file[0] + int_array_from_file[1];
Oh yes! Sweet Success
Thank you sir

I have it writing to a file

Can I assume that the strings will be the same thing?

Thanks again


My regards,



Jacob Cunningham
Topic archived. No new replies allowed.