Weird error message

This is a discussion on Weird error message within the C++ Programming forums, part of the General Programming Boards category; Well its me again. I am trying to grasp the concept of reading and writing to files and was a ...

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    5

    Weird error message

    Well its me again.

    I am trying to grasp the concept of reading and writing to files and was a bit confused on what the book was doing. So I decided to copy the example to see what was happening and I cant get the code to run. This is straight from the book and I get this error

    fatal error C1083: Cannot open include file: 'stdlib': No such file or directory

    ok so I changed it to <cstdlib> and I get the "error C2065: 'nocreate' : undeclared identifier"

    Is the book out dated?

    Code:
    #include <iostream>
    #include <fstream>
    #include <iomanip>
    #include <stdlib>    
    
    using namespace std;
    
    int main()
    {
    	const int MAXLENGTH = 21; //maximum file name length
    	const int MAXCHARS = 80; //maximum line length
    	char file1[MAXLENGTH] = "input.txt";
    	char line[MAXCHARS];
    	int ch;
    	ifstream inFile;  //ifstream constructor opens the file
    
    	inFile.open (file1, ios::nocreate);
    
    	if (inFile.fail()) //checks for a successful open
    		{
    			cout << "\nThe file was not successfully opened"
    				 << "\n Please check that the file currently exists."
    				 << endl;
    			exit (1);
    		}
    
    	cout << endl;
    
    	while( (ch = inFile.peek()) != EOF )
    		{
    			inFile.getline(line, MAXCHARS, '\n');
    			cout << line << endl;
    		}
    		
    	inFile.close();
    	cout << endl;
    
    	return 0;
    
    }

  2. #2
    Registered User
    Join Date
    Jan 2008
    Posts
    290
    To my knowledge, <stdlib> is NOT a standard C++ header. <cstdlib> is, but its part of the C library and not relevant for the example code you posted.

    I would just remove the #include.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,630
    <cstdlib> is, but its part of the C library and not relevant for the example code you posted.
    std::exit is used though, so <cstdlib> is relevant.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Nov 2009
    Posts
    7
    you could change:

    Code:
    inFile.open (file1, ios::nocreate);
    to:

    Code:
    inFile.open( file1.c_str() );
    I think at least, I'm not sure if that helps. I just finished learning about I/O to files myself.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,630
    No, file1 is a char[MAXLENGTH], not a std::string. But yes, the ios::nocreate thing should probably just be removed.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. weird things with my linked list of queue
    By -EquinoX- in forum C Programming
    Replies: 3
    Last Post: 11-22-2008, 10:23 PM
  2. weird
    By kiz in forum C Programming
    Replies: 8
    Last Post: 09-24-2007, 01:16 AM
  3. Weird Characters With GetDlgItemText
    By execute in forum Windows Programming
    Replies: 4
    Last Post: 05-04-2006, 04:53 PM
  4. weird error
    By gandalf_bar in forum Linux Programming
    Replies: 2
    Last Post: 07-17-2005, 07:32 AM
  5. Getting weird characters in Strings
    By steve8820 in forum C Programming
    Replies: 3
    Last Post: 09-18-2001, 02:49 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21