Why cant I cout or ofstream my string arrays value

Write your question here.

What's wrong I am supposed to take 4 integers from data.txt which are 1 2 3 3
and cout or ofstream the highest values for example I need in this case cout strings J and R

before last if's I can cout like cout<<vard[0]<<endl;
but in the if's I cannot.

help me pls

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

  #include <fstream>

#include <iostream>

 using namespace std;

 int main()
 {

     string vard[4]={" V "," G "," J "," R "};
     ifstream in("data.txt");


     int a,b,c,d,sk, did=-1;

     for(int i=1;i<=4;i++)
     {
         in>>sk;

         if(sk>did)
         {
             did=sk;
         }
     }

     in.close();

     ifstream inn("data.txt");
     ofstream out("rezultatai.txt");
     in>>a>>b>>c>>d;
    out<<"Daugiausiai surinko: ";

    cout<<vard[0]<<vard[1]<<vard[2]<<vard[3]<<endl;

     if(a==did)
     {
         out<<vard[0];
         cout<<vard[0]<<endl;
     }
     if(b==did)
     {
         out<<vard[1];
         cout<<vard[1]<<endl;
     }
     if(c==did)
     {
         out<<vard[2];
         cout<<vard[2]<<endl;
     }
     if(d==did)
     {
         out<<vard[3];
         cout<<vard[3]<<endl;
     }



     return 0;
 }
On line 36, what's the value of a?
What's the value of did?

Prove it by writing code to output a and output did.
I dont know why but right before line 36 a = 0, but did is ok did is 3
I noticed my mistake after i say ifstream inn i write in>> instead of inn>>
Hello Carabus,

On lines 12, 29 and 30 you define file stream variables and open files, but how so you know that they are open?

Andy
they are open until .close() is executed i think. I am a student so a begginer in c++, I need to reopen same file because the cursor is at the end and the fastest way I think is to reopen it
they are open until .close() is executed i think.


But DID they open? Did it work?

For example, imagine that I have a file called "data.txt"
ifstream in("daata.txt");
Is my file open? No. My program is looking for a file that doesn't exist.

Imagine the file is in "C:\Documents\data.txt"
ifstream in("c:\Users\data.txt");
Is my file open? No. My program is looking in the wrong directory.


Imagine that my file data.txt is in C:\Documents, but my program is running in C:\Users. My program is running in a DIFFERENT directory.
ifstream in("data.txt");
Is my file open? No. The program is looking in the wrong place.


I see that your program is just looking for "data.txt". Is that file right next to the program executable? It the program looking in the right place? Are you SURE? How do you know?

And how can you test to see if the file was opened?
Last edited on
My data.txt is in the same folder as cpp file so It works fine. I think we can cout the numbers pulled from that txt file and check it that way. But yes if the directions to the file is correct it will work fine.
You don't need to read the file twice with multiple if statements:

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
#include <fstream>
#include <iostream>
#include <iterator>

using namespace std;

int main()
{
	const string vard[4] = {" V "," G "," J "," R "};
	ifstream in("data.txt");

	if (!in.is_open()) {
		cout << "Cannot open file\n";
		return 1;
	}

	int pos {-1};

	for (int sk = 0, did = -1, i = 0; in >> sk; ++i)
		if (sk > did) {
			did = sk;
			pos = i;
		}

	if (pos >= 0 && pos < size(vard))
		cout << vard[pos] << '\n';
	else
		cout << "Number out of range\n";
}

Hello Carabus,

Sorry for the delay,

To add to what Repeater has so nicely said.

As an example: ifstream in("daata.txt");. In the first part ifstream in this will construct an object of the "ifstream" class and give you a variable to use for your input. The second part ("daata.txt") uses the overloaded ctor of the class to open the file. Or at least tries to open the file.

There are several reasons why a file will not open. Usually it is because the file name is spelled wrong or the file does not exist in the first place. And as Repeater said there could be something wrong with the path.

So just because you say ifstream in("daata.txt"); does not mean the file has opened. DO NOT COUNT on this to magically open a file. Or even find the file.

When opening a file for input or output I have found this to be helpful:
1
2
3
4
5
6
7
8
9
10
11
const std::string inFileName{ "" };  // <--- Put file name here.

std::ifstream inFile(inFileName);

if (!inFile)
{
    std::cerr << "\n     File " << std::quoted(inFileName) << " did not open.\n" << std::endl;  // <--- "std::quoted" requires header file <iomanip>.
    //std::cerr << "\n     File \"" << inFileName << "\" did not open.\n" << std::endl;

    return 1;
}

There is more than 1 way to write line 5, but this is the simplest.

In the if statement there are 2 way to use the output statement. Use whichever 1 you want and comment or delete the other. I would save the whole bit of code somewhere for future use or reference.

An "ofstream" is less likely to have a problem opening a file. First if the file does not exist it will create it, but there are still other reasons that the file could not be open. The directory or subdiretory may not be found, there may be a problem with write permissions, you may have run out of disk space to put the file. And that is what I can remember at the moment.

In you code you have:
1
2
3
in.close();

ifstream inn("data.txt");

First had you not used "inn" it would have been an error that you are trying to define the same variable a second time. For WIW this is a good place to use a variable for the file name.

Instead of your code you could use:
1
2
3
in.clear():  // <--- Resets the stream bits, so it can be used again.

in.seekg(0);  // <--- Sets the file point back to the beginning. 

This makes it easier to reuse what is already set up.

Andy
Hi Andy,

Thanks so much for the advice it was really helpful I didn't know how to reset the point to the beginning without reopening the file, now I know.
anyways I am new to this website and now I know that this community is very friendly and helpful, so I will stick around and learn :)

Thanks

My data.txt is in the same folder as cpp file so It works fine.


That's usually the mistake people make. The location of the cpp file is irrelevant. Many build systems do not put the binary executable in the same directory as the cpp file containing the main function.
Last edited on
Yeah I figured that if i don't specify the location of the txt file it needs to be right next to .exe file.
its the path you called from.
for example...
if you type this:
cd c:\
c:\somefolder\deeper\project\debug\foo.exe
it will attempt to write the file as c:\foo.file
because you *called* the program from c:\
this seems idiotic in general (there are times you may want to do this to force a file to another location) but if you make a batch file or shortcut to the program, your results may get weird because of this issue...
basically, when dealing with files, its best to get the path and file name, and make no assumptions.
Last edited on
Exactly
Topic archived. No new replies allowed.