very basic problem u can help me in a glance.

i want to read last line of the file
the name of file is stored in another file.
"output.txt" have lines, the last line of "output.txt" have the name of 2nd file.

code is here.

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
63
64
65
66
  #include <iostream>
#include <fstream>
#include <string>
#include <windows.h> //only works in windows not for unix like.
#include <cstdio>
#include <cstdlib> //boolen
#include <math.h> //math f
/////////////////////
using namespace std;

ifstream myfile; //open 1st file.
string FLine; //data of file1
ifstream myfile2; //file2
long int FLine1 ; //file data in numaric.
void Start();
void Start1();
/////////////////////? make below lines for availabel for entire program
std::string getLastLine(std::ifstream& in)
{
    std::string line;
    while (in >> std::ws && std::getline(in, line)) // skip empty lines
        ;

    return line;
}
/////////////////////? make above lines for availabel for entire program

void Start()
{
	std::ifstream myfile("output.txt"); //file open

    if (myfile.is_open() )
    {
        std::string line = getLastLine(myfile);
        std::cout << line << '\n';
        Start1();
    }
    else
        std::cout << "Unable to open file.\n";
        Sleep (3000);
        Start();
}
/////////////////////
void Start1()
{
	// the error is solved by adding "string line;" here but compiled program doesnot work as expected. dont open data file.
	myfile2.open (line.c_str() );	//error here
	if (myfile2.is_open() )
	{
	myfile2 >> FLine1 ;		//send data to FLine1
	cout << "file name " << line << " has " << FLine1 << " value \n" ;	//should show data of second file result will be 100.
	myfile2.close() ;
	Sleep (3000);
}
	else std::cout << "unable to open data file. \n";
	Start1();
}
/////////////////////
int main(int argc, char** argv)
{
cout << "starting program \n" ;
	Sleep (2000);
	Start();
	
return 0;
}
Last edited on
actually i want to use this help http://www.cplusplus.com/forum/general/108679/#msg591060
to my edit my code.
i think line # 18 to 25 from my code i really dont understand thats why i am committing mistake here in my code.
Last edited on
Change line 44: void Start1(const std::string &line)
and line 36: Start1(line);


By the way: line 41 is a recursive call of Start(). It will always be called (not just in the else case like the indention suggests). The same applies to line 56.
Even if it would be called within the else case, it doesn't make sense because the same thing will be done again and again.
Remove lines 11-13 too and use local variables instead.
The use of global variables, while apparently "easy", actually restricts a design quite much.
thanks for kind reply
let me the compile the suggestions
@coder777
I did what you said
tried to compile but
error at line 36
in function 'void Start()';
[error] too many arguments to function 'void Start1()'
Last edited on
@keskiverto
please could you please implement the suggestion in the code.
error at line 36
The prototype for Start1() on line 16 needs to be changed as well.
thank you so much @coder777.
problem solved.
Topic archived. No new replies allowed.