Fixing C++ File Program with 6 Linker Errors

  • Context: C/C++ 
  • Thread starter Thread starter chaoseverlasting
  • Start date Start date
  • Tags Tags
    C++ File Program
Click For Summary

Discussion Overview

The discussion revolves around resolving linker errors encountered in a C++ program, specifically related to file handling and variable scope. Participants explore various coding issues, compiler behavior, and the use of different headers in C++ programming.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant reports encountering six linker errors related to buffer overflows while using Borland Turbo C++ 3.
  • Another participant suggests that the line checking if the file is open should use if (!read.is_open()) instead of if (!read).
  • Concerns are raised about the use of global variables, with some participants recommending that they should be avoided unless necessary.
  • Multiple participants note the need to include #include for using cin and count, while questioning the necessity of #include.
  • One participant mentions that changing the scope of ofstream and ifstream from global to local resolved some issues, prompting questions about why this change affected program execution.
  • There is a discussion about the differences in behavior between various compilers, with some participants noting that certain variables may not be recognized as declared in different contexts.
  • Concerns are raised about the use of an outdated compiler and its potential impact on the program's compatibility with modern C++ standards.
  • Participants express confusion over the declaration of the variable i, with one participant arguing that it was declared globally and should be recognized.

Areas of Agreement / Disagreement

Participants express differing views on the necessity of certain headers and the implications of using global versus local variables. There is no consensus on the best practices for resolving the linker errors, and multiple competing perspectives remain throughout the discussion.

Contextual Notes

Limitations include unresolved issues related to the specific behavior of the Borland Turbo C++ 3 compiler, the handling of global versus local variables, and the potential discrepancies in library implementations across different compilers.

Who May Find This Useful

This discussion may be useful for programmers dealing with C++ file handling, variable scope issues, and those using older compilers who are seeking to understand modern coding practices and standards.

chaoseverlasting
Messages
1,051
Reaction score
3
Im having a problem with a cpp program. It gives me 6 linker errors, which have something to do with buffer overflows. I used borland turbo cpp 3 to compile this. What should I do. Here's the code:

#include<fstream.h>
#include<conio.h>
#include<stdio.h>



ofstream write;
ifstream read;

int i;

void main()
{
fflush(stdin);
write.open("c:\windows\test.txt"); //open file for output
char put1[80], get1[80];

count<<"Enter a sentence (<80): ";
cin>>put1;
for(i=0;i<80;i++)
write.put(put1);
write.put('\n');

write.close();

read.open("c:\windows\test.txt", ios::in);

if(!read)
{
count<<"File cannot be opened";
}
read.seekg(0);

while(!read.eof())
{
read.getline(get1,80,'\n');
count<<get1<<"\n";
}
read.close();
}
 
Technology news on Phys.org
I don't know why your linker fails. I note the following issues with your program.
1. the line if(!read) should be if (!read.is_open ())
2. if the file cannot be opened for reading, the programs goes ahead and reads it anyway.
3. in the line read.open("c:\windows\test.txt", ios::in); the second parameter is redundant because read is declared as ifstream, not fstream. I note that you do not have a similar second parameter when you call the open method of write.

In addition, I note that you have three global variables, but none of them need to be global. I recommend that you don't declare globals except when necessary. With experience you will find that globals are a mixed blessing at best and rarely necessary.
 
I'm not sure what you're tryign to do but I see a few problems. If you use "count" and "cin", you will need to have "#include<iostream>" at the top. I also think that the head "#incude<conio.h>" might not be necessary. Try compiling the following, and see if it is an improvement.

#include<fstream>
#include<iostream>

using namespace std;

int main()
{
int i;
fflush(stdin);
ofstream write("c:\\windows\\test.txt"); //open file for output
char put1[80], get1[80];

count<<"Enter a sentence (<80): ";
cin>>put1;
for(i=0;i<80;i++)
write.put(put1);
write.put('\n');

write.close();

ifstream read("c:\\windows\\test.txt", ios::in);

if(!read)
{
count<<"File cannot be opened";
}
read.seekg(0);

while(!read.eof())
{
read.getline(get1,80,'\n');
count<<get1<<"\n";
}
read.close();

return 0;
}
 
dimensionless said:
I'm not sure what you're tryign to do but I see a few problems. If you use "count" and "cin", you will need to have "#include<iostream>" at the top. I also think that the head "#incude<conio.h>" might not be necessary.

Fstream is derived from iostream and hence already has cin and count defined. However, when I changed ofstream and ifstream from global to local, then the program executed. Why is that? It seems to be really stupid that something small like that would affect the program. What could the reason be?
 
jimmysnyder said:
I don't know why your linker fails. I note the following issues with your program.
1. the line if(!read) should be if (!read.is_open ())
If opening the file fails, it sets the fail bit, and thus !read will return true.

chaoseverlasting said:
Fstream is derived from iostream and hence already has cin and count defined.
The header <fstream> is not required to include the header <iostream>. In fact, I rather expect it's not allowed to do so -- so if your version does, that's an error in the library.
 
Last edited:
Hurkyl said:
If opening the file fails, it sets the fail bit, and thus !read will return true.


The header <fstream> is not required to include the header <iostream>. In fact, I rather expect it's not allowed to do so -- so if your version does, that's an error in the library.

Im using turbo c++, and I've checked the help where it shows that iostream is also contained within fstream (iostream is inherited by fstream I guess).

After I made the ofstream and ifstream class objects local rather than global, and made some other minor changes, the program executes, but I get two warnings, and it crashes after accepting the string. Here's the code:


#include<fstream.h>
#include<conio.h>
#include<stdio.h>


int i;

void main()
{
ofstream write;
ifstream read;

fflush(stdin);
write.open("c:\\test.txt"); //open file for output

if(!write)
count<<"File cannot be opened";

char put1[80], get1[80];

count<<"Enter a sentence (<80): ";
cin>>put1;

for(i=0;i<80;i++)
write.put(put1);

write.put('\n');

write.close();

read.open("c:\\test.txt");

if(!read)
count<<"File cannot be opened";

read.seekg(0);

while(read)
{
read.getline(get1,80,'\n');
count<<get1<<"\n";
}
read.close();
}
 
Most compilers would tell you that the variable i in function main was never declared. The following seems to work in GCC in Windows#include<iostream>
#include<fstream>
using namespace std;

int main()
{
int i;
ofstream write;
ifstream read;

fflush(stdin);
write.open("c:\\test.txt"); //open file for output

if(!write)
count<<"File cannot be opened";

char put1[80], get1[80];

count<<"Enter a sentence (<80): ";
cin>>put1;

for(i=0;i<80;i++)
write.put(put1);

write.put('\n');

write.close();

read.open("c:\\test.txt");

if(!read)
count<<"File cannot be opened";

read.seekg(0);

while(read)
{
read.getline(get1,80,'\n');
count<<get1<<"\n";
}
read.close();

return 0;
}
 
dimensionless said:
Most compilers would tell you that the variable i in function main was never declared. The following seems to work in GCC in Windows


#include<iostream>
#include<fstream>
using namespace std;

int main()
{
int i;
ofstream write;
ifstream read;

fflush(stdin);
write.open("c:\\test.txt"); //open file for output

if(!write)
count<<"File cannot be opened";

char put1[80], get1[80];

count<<"Enter a sentence (<80): ";
cin>>put1;

for(i=0;i<80;i++)
write.put(put1);

write.put('\n');

write.close();

read.open("c:\\test.txt");

if(!read)
count<<"File cannot be opened";

read.seekg(0);

while(read)
{
read.getline(get1,80,'\n');
count<<get1<<"\n";
}
read.close();

return 0;
}

Whats the difference? The variable i was declared globally before, and now its local, and main is an integer function whereas it was a void function before. And most compilers would tell me that the variable i wasnt declared? Are you blind? Whats that in the beginning of the code? Just before main? Isnt i declared globally as an integer? Come on dude.

I admit that declaring the classes ofstream and ifstream locally rather than globally solved the linker problem, but that's probably because global variables are given a different part of the memory and local variables a different part. For some reason, the code couldn't access one part of the memory allocated.

I still can't get over the "most compilers would tell you the variable i wasnt declared". Gimme a break.
 
chaoseverlasting said:
I used borland turbo cpp 3 to compile this.
Just so you're aware, you're using an archaic compiler, which I suspect comes with an archaic standard library, and nonstandard extensions. (e.g. conio.h) So the results you get may be at odds with others' expectations.
 
Last edited:
  • #10
I used borland turbo cpp 3
That was released in 1991. It's been 16 years, get a newer compiler (at least post 1998 standard).

I've checked the help where it shows that iostream is also contained within fstream (iostream is inherited by fstream I guess).
I'll go with what the standard actually says rather than some help file. Anyhow, according to the standard, including <fstream> does not mean the compiler have to make available to the end user what's in <iostream>. i.e. including <fstream> doesn't necessarily include <iostream>.

Code:
#include<fstream.h>
Pre-standard header. It should now be
Code:
#include <fstream>

Code:
#include<conio.h>
Old and non-standard header file. No need for it, bin it.

Code:
#include<stdio.h>
Don't mix and match C and C++ IO's.

Code:
void main()
Non-standard. main() have to returns int. That's the case whether you're programming in C or C++.

Code:
fflush(stdin);
1. You've just started the program, you don't need to flush anything. 2. You're mixing C and C++ IO's.

Code:
read.seekg(0);
0 + 0 = 0


Code:
#include <iostream>
#include <fstream>
#include <string>

int main()
{
    std::ofstream write;
    write.open("C:/test.txt");

    if(!write)
        std::cout << "File cannot be opened";

    std::string put1, get1;

    std::cout << "Enter a sentence: ";
    std::getline( std::cin, put1 );

    write << put1;

    write.close();

    std::ifstream read;

    read.open("C:/test.txt");

    if(!read)
        std::cout << "File cannot be opened";

    while( read )
    {
        getline( read, get1 );
        std::cout << get1 << '\n';
    }
    read.close();

    return 0;
}
 
  • #11
#include <iostream>
#include <fstream>
#include <string>

int main()
{
std::ofstream write; //What does std stand for?
write.open("C:/test.txt");

if(!write)
std::count << "File cannot be opened";

std::string put1, get1;

std::count << "Enter a sentence: ";
std::getline( std::cin, put1 );

write << put1;

write.close();

std::ifstream read;

read.open("C:/test.txt");

if(!read)
std::count << "File cannot be opened";

while( read )
{
getline( read, get1 );
std::count << get1 << '\n';
}
read.close();

return 0;
}



What does std stand for? Like, std::count... why not just count?

Ok. If you guys say its out dated, Ill get Bloodshed Dev C++. Thank you all so far.
 
  • #12
Ok. Now, I've got Bloodshed cpp, but I'm having some problems. Where is the getch() (or similar) function defined in these libraries?

There is a feature to find out what functions are in which header files but I don't know how to use it. Could someone tell me where the tab is?

Im running this thing, but the output goes by too fast for me to see it (hence my asking for the getch() funtion). I know this is pretty basic stuff, but could someone please help me out?
 
  • #13
std is a namespace. Rather than explain what a namespace is, I prefer that you look this up somewhere as my explanation would probably be incomplete.

You must either give full names to members in a namespace as follows:

Code:
#include <iostream>

int main()
{
    std::cout << "Hello world!" << std::endl;
    return 0;
}

or announce that you want the namespace implied as follows:

Code:
#include <iostream>

using namespace std;

int main()
{
    cout << "Hello world!" << endl;
    return 0;
}

My custom is to have the using declaration in my .cpp files, but not in my header files because I don't want to impose the namespace on my clients.

Because the use of namespaces allows name clashes, there may be times when you need to disambiguate. For instance, if you are including some files in the jimmy namespace and there is a member called endl in that namespace then the following code will have a problem:

Code:
#include <iostream>
#include <jimmy.h>

using namespace std;
using namespace jimmy;

int main()
{
    cout << "Hello world!" << endl;
    return 0;
}

The compiler doesn't know which endl to use. In that case, merely change one line:

cout << "Hello world!" << std::endl;

or

cout << "Hello world!" << jimmy::endl;

whichever is suitable.
 
Last edited:
  • #14
chaoseverlasting said:
Ok. Now, I've got Bloodshed cpp, but I'm having some problems. Where is the getch() (or similar) function defined in these libraries?
getch() is not part of C -- it was an extension that Borland provided. However, getchar() is a part of C. And, of course, cin.get() is part of C++.
 
  • #15
Quote:
Originally Posted by chaoseverlasting View Post
Ok. Now, I've got Bloodshed cpp, but I'm having some problems. Where is the getch() (or similar) function defined in these libraries?

getch() is not part of C -- it was an extension that Borland provided. However, getchar() is a part of C. And, of course, cin.get() is part of C++.

Thank you! I wish our school system would get updated so I'd learned this then. Its seems very stupid now to be using these old conventions, what's worse, I didnt even know they were old!

std is a namespace. Rather than explain what a namespace is, I prefer that you look this up somewhere as my explanation would probably be incomplete.

You must either give full names to members in a namespace as follows:

Code:

#include <iostream>

int main()
{
std::count << "Hello world!" << std::endl;
return 0;
}

or announce that you want the namespace implied as follows:
...

Thank you again. That makes it a lot clearer. So, cin, and count are defined in the standard namespace, but not in iostream? Or is it that the working is defined in iostream and the names in std namespace?
 
  • #16
How would you get the program to stop long enough for me to see the output? In borland, there was also a delay() function, which doesn't work here. Can anyone suggest a decent book which I could buy?
 
  • #17
chaoseverlasting said:
So, cin, and count are defined in the standard namespace, but not in iostream?
cin and count are defined as std::cin and std::count in iostream. The statement
using namespace std;
allows you to call them by their shorter names.
 
  • #18
Ok. But what about making the program stop/delay/hang on long enough so that i can see the output? I want to make sure that the read function i wrote actually reads the files and prints the contents.
 
  • #19
chaoseverlasting said:
Ok. But what about making the program stop/delay/hang on long enough so that i can see the output? I want to make sure that the read function i wrote actually reads the files and prints the contents.
Can you redirect the output to a file?
 
  • #20
Or you can do what you're suppose to do with a console program, run it from within a console prompt.

Alternatively, use a method, any method, that keep the program from finishing before the end user (i.e. you) have time to read the output.

  • Add the line system("PAUSE"); to the end of the program just before the return statement. The function std::system( const char* ) is declare in <cstdlib>, it takes a C-style character string and executes it as if it was entered into the command promt*. PAUSE is a windows program that writes a note to the console that the user should press a key to continue and then waits until the user presses a key. (Side effect: The string "Press any key to continue..." is localized, meaning for example on a French Windows version the string would be "Appuyez sur une touche pour continuer...")
  • Read some dummy variable. This only works if there's nothing in the input buffer because otherwise the buffer content is read before the user is asked for input.
  • Tell cin to ignore all input until Enter is pressed. (Again, only works if there's nothing already in the input buffer.) This can be done with
    cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); (std::numeric_limits is declared in <limits> [think])
  • Hope that the input buffer is empty and simply read a character. This works especially well for output only programs. This can be done with cin.get()
 
  • #21
Ive tried some of the things you guys said, but the program won't stop for any input. After it accepts the string, it just closes. It doesn't print the "file can't be opened" code, it doesn't stop before the read funtion, but when I open the file c:\test.txt, the string I entered is in there. Whats the goof up in this?

#include<fstream>
#include<iostream>
#include<cstdlib>using namespace std;

int i;

int main()
{
ofstream write;
ifstream read;

fflush(stdin);
write.open("c:\\test.txt"); //open file for output

if(!write)
count<<"File cannot be opened";

char put1[80], get1[80];

count<<"Enter a sentence (<80): ";
cin>>put1;

for(i=0;i<80;i++)
write.put(put1);

write.put('\n');

write.close();

read.open("c:\\test.txt");

if(!read)
{
count<<"File cannot be opened";
cin.get();
}
read.seekg(0);
count<<"reading file";
cin.get();
while(read)
{
read.getline(get1,80,'\n');
count<<get1<<"\n";
}

getchar();
cin>>put1[79];

read.close();
return 0;
}
 

Similar threads

  • · Replies 6 ·
Replies
6
Views
12K
Replies
12
Views
2K
Replies
14
Views
4K
  • · Replies 89 ·
3
Replies
89
Views
6K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 15 ·
Replies
15
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 14 ·
Replies
14
Views
4K
  • · Replies 15 ·
Replies
15
Views
4K