Fixing C++ File Program with 6 Linker Errors

In summary, the conversation is about a problem with a CPP program that is giving 6 linker errors related to buffer overflows. The program was compiled using Borland Turbo CPP 3. The expert points out several issues in the code, including redundant parameters, unnecessary global variables, and missing headers. The expert also provides a revised code that solves the linker problem but still has some warnings and crashes after accepting input. The conversation ends with a disagreement about a variable declaration.
  • #1
chaoseverlasting
1,050
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];

cout<<"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)
{
cout<<"File cannot be opened";
}
read.seekg(0);

while(!read.eof())
{
read.getline(get1,80,'\n');
cout<<get1<<"\n";
}
read.close();
}
 
Technology news on Phys.org
  • #2
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.
 
  • #3
I'm not sure what you're tryign to do but I see a few problems. If you use "cout" 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];

cout<<"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)
{
cout<<"File cannot be opened";
}
read.seekg(0);

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

return 0;
}
 
  • #4
dimensionless said:
I'm not sure what you're tryign to do but I see a few problems. If you use "cout" 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 cout 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?
 
  • #5
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 cout 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:
  • #6
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)
cout<<"File cannot be opened";

char put1[80], get1[80];

cout<<"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)
cout<<"File cannot be opened";

read.seekg(0);

while(read)
{
read.getline(get1,80,'\n');
cout<<get1<<"\n";
}
read.close();
}
 
  • #7
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)
cout<<"File cannot be opened";

char put1[80], get1[80];

cout<<"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)
cout<<"File cannot be opened";

read.seekg(0);

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

return 0;
}
 
  • #8
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)
cout<<"File cannot be opened";

char put1[80], get1[80];

cout<<"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)
cout<<"File cannot be opened";

read.seekg(0);

while(read)
{
read.getline(get1,80,'\n');
cout<<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.
 
  • #9
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::eek:fstream write; //What does std stand for?
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;
}



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

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::cout << "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 cout 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 cout are defined in the standard namespace, but not in iostream?
cin and cout are defined as std::cin and std::cout 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)
cout<<"File cannot be opened";

char put1[80], get1[80];

cout<<"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)
{
cout<<"File cannot be opened";
cin.get();
}
read.seekg(0);
cout<<"reading file";
cin.get();
while(read)
{
read.getline(get1,80,'\n');
cout<<get1<<"\n";
}

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

read.close();
return 0;
}
 

1. Why am I getting linker errors in my C++ file?

Linker errors occur when the compiler is unable to find the definition of a function or variable that is being used in your code. This can happen if the function or variable is declared but not defined, or if there is a typo in the name of the function or variable.

2. How do I fix linker errors in my C++ program?

To fix linker errors, you need to make sure that all the functions and variables used in your code are properly declared and defined. Check for any typos in the names of functions or variables, and make sure they are all spelled correctly. You may also need to include additional header files or libraries in your code.

3. Can I get linker errors if I am missing a semicolon in my C++ code?

Yes, a missing semicolon can cause linker errors in your C++ code. This is because the compiler expects a semicolon at the end of each statement, and if it is missing, the code may not compile properly, resulting in linker errors.

4. What is the difference between a linker error and a syntax error in C++?

A linker error occurs when the compiler is unable to find the definition of a function or variable, while a syntax error occurs when there is a mistake in the structure or grammar of your code. Linker errors are usually easier to fix as they are related to the code itself, whereas syntax errors may require a deeper understanding of the language.

5. How can I prevent linker errors in my C++ program?

To prevent linker errors, it is important to make sure that all functions and variables used in your code are properly declared and defined. It is also a good practice to test your code frequently and fix any errors as soon as they are identified. Additionally, keeping your code organized and following best practices can help prevent linker errors in your C++ program.

Similar threads

  • Programming and Computer Science
Replies
6
Views
8K
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
3
Replies
89
Views
4K
  • Programming and Computer Science
Replies
14
Views
2K
  • Programming and Computer Science
Replies
6
Views
2K
  • Programming and Computer Science
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
15
Views
2K
  • Programming and Computer Science
Replies
4
Views
2K
  • Programming and Computer Science
Replies
4
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
14
Views
2K
Back
Top