Using Visual C++; how to read from an infile?

This is a discussion on Using Visual C++; how to read from an infile? within the C++ Programming forums, part of the General Programming Boards category; How do I read from an inFile using Visual C++? I am used to using inFile << intNum; But this ...

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    34

    Using Visual C++; how to read from an infile?

    How do I read from an inFile using Visual C++? I am used to using inFile << intNum;
    But this will not work with this compiler...
    The only thing I know how to get is a char with inFile.get(char)

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    Funny -- I do it all the time with that compiler. Post your code and I'll guarentee that YOU made the mistake, not the compiler.

    inFile << intNum <<< wrong (this is output stream)
    inFile >> intNum <<< right
    Last edited by Ancient Dragon; 09-27-2005 at 08:23 AM.

  3. #3
    Registered User
    Join Date
    Mar 2005
    Posts
    34
    Yeah, your right, oh well, now I got another error. YEAH!

  4. #4
    Registered User
    Join Date
    Mar 2005
    Posts
    34
    Now i'm getting:
    : error C2475: 'std::basic_istream<_Elem,_Traits>::get' : forming a pointer-to-member requires explicit use of the address-of operator ('&') and a qualified name
    with
    [
    _Elem=char,
    _Traits=std::char_traits<char>
    ]
    and:
    : error C2297: '>>' : illegal, right operand has type 'int [100]'


    here is my code:
    Code:
    [red]Binary.h[/red]
    #include<iostream>
    #include<fstream>
    
    using namespace std;
    
    class BinaryFile
    {
    public:
    	void readFromFile(ifstream& inFile, BinaryFile rd);
    	void instertBinary(BinaryFile rd);
    	void validRead(BinaryFile rd);
    	void writeBinary(BinaryFile rd, ofstream& outFile);
    
    	int acctNum[100];
    	float acctBalance;
    };
    
    [red]binary.cxx[/red]
    #include "Binary.h"
    
    
    void BinaryFile::readFromFile(ifstream& inFile, BinaryFile rd)
    {
    	int holder;
    	holder = inFile.peek();
    //	if (holder = -1)
    //		continue;
    //	else
    		inFile.get >> acctNum >> acctBalance;
    }
    
    void BinaryFile::writeBinary(BinaryFile rd, ofstream& outFile)
    {
    	outFile.seekp(rd.acctNum % 97);
    	outFile.write((char*)&rd);
    }
    
    [red]runBinary.cxx[/red]
    #include "Binary.h"
    
    
    int main()
    {
    	ifstream inFile;
    //	ofstream outFile;
    	inFile.open("in.txt");
    //	outFile.open("out.txt");
    
    	ofstream outFile("binrecords.bin");
    	BinaryFile blankRd;
    	for (int i = 1; i <= 100; i++)
    	{
    		outFile.write((char*)&blankRd, sizeof(blankRd));
    	}
    
    	BinaryFile rd;
    	rd.readFromFile(inFile,rd);
    }
    any ideas?

  5. #5
    Registered User
    Join Date
    Mar 2005
    Posts
    34
    sorry, figured out my stupidity

  6. #6
    Registered User
    Join Date
    Nov 2004
    Posts
    69
    Quote Originally Posted by advocation
    sorry, figured out my stupidity
    We all make mistakes

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. bytes lost with partial read in UDP
    By mynickmynick in forum Networking/Device Communication
    Replies: 3
    Last Post: 03-31-2009, 02:06 AM
  2. evaluate postfix expressions read from infile
    By killmequick in forum C Programming
    Replies: 5
    Last Post: 10-02-2008, 01:19 PM
  3. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 04:02 AM
  4. Trouble Reading Data from Files
    By CConfusion in forum C Programming
    Replies: 11
    Last Post: 04-06-2006, 07:12 PM
  5. Stupid compiler errors
    By ChrisEacrett in forum C++ Programming
    Replies: 9
    Last Post: 11-30-2003, 04:44 PM

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