Thread: iostream problem

  1. #1
    Registered User
    Join Date
    Aug 2023
    Posts
    45

    Question iostream problem

    char data[100] ;


    // opening a file in the write mode.
    ofstream outfile ;
    outfile.open( " Demo2.txt " );
    cout << " Writing to the file Demo2.txt" << endl;


    cout << " Enter your name : ";
    cin.getline(data,100);
    // writing the input data into the outfile.
    outfile << data << endl;


    cout << " Enter your age: ";
    cin >> data;
    cin.ignore();
    // writing the input data into the outfile.
    outfile << data << endl;


    // closing the opened file.
    outfile.close(); //this much works!


    // opening a file in read mode.
    ifstream infile;
    infile.open("Demo2.txt ");
    cout << " Reading from the file Demo2.txt " << endl;
    infile >> data;


    // writing the data
    cout << data << endl;


    // reading the data from the file
    infile >> data;
    cout << data << endl;


    // closing the opened file.
    infile.close();


    The first half of this code works. The second half shows the 2nd entry (2x) from the Demo2.txt file and not the name and number in the Demo2.txt file. In the debugger, data shows that the number seems to be overriding the name. A little perplexed here.
    maxcy

  2. #2
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,111
    Your code is C++!!!

    This is a C Programming Language forum!!!

    Please post any C++ Code questions in the C++ forum on this site.

  3. #3
    Registered User
    Join Date
    Dec 2017
    Posts
    1,633
    You do realize this is C++, not C, right?
    Anyway, your first file name is " Demo2.txt " (with a space before and after it, for some reason) and your second file name is "Demo2.txt " (with a space after it but not before it).
    Maybe that has something to do with it?
    You seem to have a lot of random spaces before and after your strings.
    It prints the age twice since that's the last thing you entered into the data array and since it didn't open the file for input (because the file name was different) nothing was read into the data array from the file.

    Also, remember to use code tags when posting code, and post the complete code (with includes and int main, etc.).
    Last edited by john.c; 08-24-2023 at 11:51 AM.
    A little inaccuracy saves tons of explanation. - H.H. Munro

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Moved to the correct forum.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Aug 2023
    Posts
    45

    2 errors left to resolve

    First problem: the boolean false variable was used by including stdbool in C. But I think that true and false are both part of the std library in modern C++ compilers. If that is true then the cons int for true and false in this project in misc.h is a problem and is why the compiler complains about this line of code. So just get rid of misc.h?

    Second problem: buffer.cpp contains "#include <iostream.h>". Compiler says no such file or directory! Are these .h include files just meant for c programs? And the same version without the .h the C++ version? I thought the .h versions were meant to be used in C++ programs as some sort of compatibility to c code.

    See attached code.

    Some of my projects contain about 25 .cpp & .h files. Do you really think attaching such a large project is a good idea?
    maxcy / wt1v
    Attached Files Attached Files

  6. #6
    Registered User
    Join Date
    Aug 2023
    Posts
    45
    thanks!!!!

  7. #7
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,111
    Quote Originally Posted by maxcy View Post
    First problem: the boolean false variable was used by including stdbool in C. But I think that true and false are both part of the std library in modern C++ compilers. If that is true then the cons int for true and false in this project in misc.h is a problem and is why the compiler complains about this line of code. So just get rid of misc.h?

    Second problem: buffer.cpp contains "#include <iostream.h>". Compiler says no such file or directory! Are these .h include files just meant for c programs? And the same version without the .h the C++ version? I thought the .h versions were meant to be used in C++ programs as some sort of compatibility to c code.

    See attached code.

    Some of my projects contain about 25 .cpp & .h files. Do you really think attaching such a large project is a good idea?
    maxcy / wt1v
    You don't have enough knowledge about C & C++ to be trying to compile this code. If you did, you would not ask a lot of the questions you have asked.

    Put the book on the shelf and study one of the C Programming books I have listed in my previous post on the C forum.

    Then study a good up to date book on the C++ Programming language.

    Then, and only then, you may be ready to attack this book you are pulling code from.

  8. #8
    Registered User
    Join Date
    Dec 2017
    Posts
    1,633
    Is this from the same garbage book?
    Why are you using such an old terrible book?
    Learning C++ from a book from 1991 is insane.
    The minimum standard to learn today is C++11 (2011).
    And probably it should be at least C++17 (2017).
    There's also C++20 and now C++23 (although the very latest standards take a while to be fully implemented).

    Some points on your current code:

    In modern code, filenames are not usually all uppercase.
    At most, the first letter of each word could be uppercase.
    And the .cpp and .h suffixes should definitely be all lowercase.

    In C++, standard library header files should not end in .h, so e.g., iostream.h should just be iostream. If they are from the C subset of the library they should also not end in .h. Instead you change, e.g., stdio.h to cstdio, stdlib.h to cstdlib, time.h to ctime, ctype.h to cctype, etc. This also requires that you either say
    Code:
    using namespace std;
    after the includes or put std:: in front of the library entities.

    I think that true and false are both part of the std library in modern C++ compilers
    They are actually part of the language core, not the standard library. In modern C they are part of the standard library in stdbool.c.
    I imagine that true and false have always been keywords in C++ (even in 1991, but maybe not).

    There are other errors (like abortMsg should be const), but as rstanley says, if you actually want to learn C++ then you need to get a modern book (or online tutorial) and go through it from the beginning.
    A little inaccuracy saves tons of explanation. - H.H. Munro

  9. #9
    Registered User
    Join Date
    Aug 2023
    Posts
    45

    Question

    There aren't that many good books on compiler theory!!! These 2 old books are quite good!! Holub was a disaster!

    Some points on your current code:

    In modern code, filenames are not usually all uppercase.
    At most, the first letter of each word could be uppercase.
    And the .cpp and .h suffixes should definitely be all lowercase.

    In C++, standard library header files should not end in .h, so e.g., iostream.h should just be iostream. If they are from the C subset of the library they should also not end in .h. Instead you change, e.g., stdio.h to cstdio, stdlib.h to cstdlib, time.h to ctime, ctype.h to cctype, etc. This also requires that you either say
    Code:
    using namespace std;
    after the includes or put std:: in front of the library entities.
    I know this! Done! See my attached zip code! Only problem now left is with the file code! nocreate was deprecated and I replaced it with noreplace.


    They are actually part of the language core, not the standard library. In modern C they are part of the standard library in stdbool.c.
    I imagine that true and false have always been keywords in C++ (even in 1991, but maybe not). This much I have learned by fixing these examples!

    I've got 4 sites that I routinely visit that are a great help learning this. Several sites on design patterns alone. And When I fooled around with some of these 'modern' examples I quickly learned about the various compiler versions. string_view got me started leaning about compiler versions and I had to go back and update my minGW compiler suite to get these examples to work. I don't know of any C++ book that would get into this kind of subject matter!!!

    BTW you might like to know that I am no beginner! I've spent years developing assembly code at PWA and also used LabVIEW and c. I doubt many on this forum have my kind of experience in these endeavors!

    Search for /++ (attached zip file of buffer) in buffer.cpp and buffer.h to see the problem areas! All the rest of the code files I have compiled and ready to run! Seems like my only remaining problem is fixing the file errors and they may be related to including the proper libraries.
    maxcy
    Attached Files Attached Files

  10. #10
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,111
    " I've spent years developing assembly code at PWA and also used LabVIEW and c.:

    You may have "used C" but you don't begin to understand C or C++. Files are traditionally all lower case! You are not paying attention to the error messages shown by the compiler, if you have turned on the compiler warning levels, and turned them up to the highest level!!!

    You have a basic error in the zip file code, that you have already been told how to correct!!!

    How can you understand "Compiler Theory" without first learning the programming languages the compiler is designed to compile???

    For gcc & g++, please study this man page!!!

  11. #11
    Registered User Sir Galahad's Avatar
    Join Date
    Nov 2016
    Location
    The Round Table
    Posts
    277
    Quote Originally Posted by maxcy View Post
    BTW you might like to know that I am no beginner! I've spent years developing assembly code at PWA and also used LabVIEW and c. I doubt many on this forum have my kind of experience in these endeavors!
    And yet you get tripped up on such trivial errors which have been so plainly spelled out to you in the compiler's output?

    Look, I started learning C++ maybe one or two years after I started programming (in C). In a week's time, I was already stretching the language to its limits, creating and exploring all manner of interesting mechanisms. If the compiler choked on an error, it was for a very complicated and subtle reason. Not because I couldn't some ancient relic of an example to compile. Because I was way, way passed all of that.

    So maybe you do have a lot of experience in this or that kind of programming, but at the end of the day your skills are frankly sub-par at best. Which isn't to say that you aren't capable of improvement. Of course you can improve with practice over time.

    But you really should start by humbly recognizing your weaknesses. I'm not the best at skiing either, but I do acknowledge that fact, and as such the quality of my improvements grow from that acceptance. Point being, denial only hinders you on the path to improvement....

  12. #12
    Registered User
    Join Date
    Aug 2023
    Posts
    45
    "iostream' no such file or..." - C++ Search results This link shows that I'm not the only one with this problem! The book I am working on has 14 chapters and I got 13 of them to compile and run after updating the code. Only one chapter has problems: chapter 1. My method: change all K & R parameter lists to new type, use namespace std all over, the compiler tells what includes are needed (done), function prototypes at the top of all files (some code had missing forward references), changed deprecated items ie nocreate to noreplace and fixed true/false problem. Design patterns, algorithms, and compiler design are language independent. JAVA and UML and Gang of Four book have nothing to do with c or C++. I don't know of any books on using compilers. I just ran into this topic when I was investigating a string_view code example and my compiler went nuts! Wrong compiler version. So now I have to figure out how to get my IDE to use g++ instead of GCC?

  13. #13
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,111
    Please post the code that shows the iostream issue.

  14. #14
    Registered User
    Join Date
    Aug 2023
    Posts
    45

    Talking 3 new files

    This zip file contains 3 files. main.cpp is an example of fstream usage and it does compile, so my compiler can't be the problem. The buffer.cpp and buffer.h files are the only 2 files in my project that won't compile. In these files you can search for // +=+ and the lines that the compiler complains about are the lines marked with the above marker to make it easier to find. I could send a snippet of the error messages in the compiler log, but they pertain to 'file' not being declared or 'fstream' does not name a type.
    Attached Files Attached Files

  15. #15
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    There are a couple of problems.

    1. You didn't provide common.h.
    This is probably where all the "abort" things come from in the reduced error messages.

    2. In header files, you need to use the fully qualified namespace.

    3. ios::noreplace is a C++23 feature only.
    cppreference.com is an excellent resource for tracking when things got added (or removed) from the ever shifting sands of the C++ language spec.

    My diffs and reduced list of error messages.
    Code:
    diff --git a/BUFFER.CPP b/BUFFER.CPP
    index c51488a..603cc03 100644
    --- a/BUFFER.CPP
    +++ b/BUFFER.CPP
    @@ -23,7 +23,7 @@
     #include <iostream>
     #include <fstream>
     #include <ctime>
    -#include "common.h"
    +//#include "common.h"
     #include "buffer.h"
    
     using namespace std;
    @@ -54,7 +54,9 @@ TTextInBuffer::TTextInBuffer(const char *pInputFileName, TAbortCode ac)
         strcpy(pFileName, pInputFileName);
    
         //--Open the input file.  Abort if failed.
    -    file.open(pFileName, ios::in ! ios::noreplace); // +=+
    +    file.open(pFileName, ios::in /*!! | ios::noreplace */ ); // +=+ use bitwise-or
    +    //!! ios::noreplace is C++23 and later
    +    //!! https://en.cppreference.com/w/cpp/io/ios_base
         if (!file.good()) AbortTranslation(ac); // +=+
     }
    
    diff --git a/BUFFER.H b/BUFFER.H
    index 9e7951a..0d4eaa6 100644
    --- a/BUFFER.H
    +++ b/BUFFER.H
    @@ -43,7 +43,11 @@ const int maxInputBufferSize = 256;
     class TTextInBuffer {
    
     protected:
    -    fstream  file;                      // input text file +=+ 'fstream' does not name a type
    +    //!! Need to use std:: (or whatever the appropriate namespace is when
    +    //!! referring to symbols in header files.
    +    //!! DO NOT have   using namespace std;   in header files as a quick
    +    //!! get out of jail crutch.  It will hurt you bad later on.
    +    std::fstream  file;                      // input text file +=+ 'fstream' does not name a type
         char    *const pFileName;           // ptr to the file name
         char     text[maxInputBufferSize];  // input text buffer
         char    *pChar;                     // ptr to the current char
    
    
    
    In file included from BUFFER.CPP:27:
    buffer.h:59:47: error: 'TAbortCode' has not been declared
         TTextInBuffer(const char *pInputFileName, TAbortCode ac);
                                                   ^~~~~~~~~~
    BUFFER.CPP:50:58: error: 'TAbortCode' has not been declared
     TTextInBuffer::TTextInBuffer(const char *pInputFileName, TAbortCode ac)
                                                              ^~~~~~~~~~
    BUFFER.CPP: In constructor 'TTextInBuffer::TTextInBuffer(const char*, int)':
    BUFFER.CPP:60:23: error: 'AbortTranslation' was not declared in this scope
         if (!file.good()) AbortTranslation(ac); // +=+
                           ^~~~~~~~~~~~~~~~
    BUFFER.CPP: In constructor 'TSourceBuffer::TSourceBuffer(const char*)':
    BUFFER.CPP:125:38: error: 'abortSourceFileOpenFailed' was not declared in this scope
         : TTextInBuffer(pSourceFileName, abortSourceFileOpenFailed)
                                          ^~~~~~~~~~~~~~~~~~~~~~~~~
    BUFFER.CPP: In member function 'virtual char TSourceBuffer::GetLine()':
    BUFFER.CPP:153:37: error: 'currentLineNumber' was not declared in this scope
      if (listFlag) list.PutLine(text, ++currentLineNumber,
                                         ^~~~~~~~~~~~~~~~~
    BUFFER.CPP:154:8: error: 'currentNestingLevel' was not declared in this scope
            currentNestingLevel);
            ^~~~~~~~~~~~~~~~~~~
    Oh, and do yourself a favour by replacing all those char* pointers (and bad attempts at memory management) with std::string instances.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. IOSTREAM problem
    By joan in forum C++ Programming
    Replies: 13
    Last Post: 12-01-2006, 02:56 PM
  2. Problem during compile (iostream.h error)
    By JoJo in forum C Programming
    Replies: 4
    Last Post: 04-29-2003, 06:58 PM
  3. iostream problem. Help! :P
    By skyvoyager in forum C++ Programming
    Replies: 4
    Last Post: 08-05-2002, 10:14 PM
  4. iostream.h help???
    By chaoticseedalpha in forum C++ Programming
    Replies: 10
    Last Post: 01-29-2002, 10:12 AM
  5. Iostream
    By Staticman in forum Game Programming
    Replies: 2
    Last Post: 10-16-2001, 03:48 PM

Tags for this Thread