Thread: Reading from a files as a string

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    http://faq.cprogramming.com/cgi-bin/...&id=1043284351
    Btw, you can use a virtual machine for testing your app at home.
    Although the link is for C, the same solution applies to C++ as well (reading using the >> operator).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  2. #2
    Registered User
    Join Date
    Nov 2007
    Posts
    40
    Setting up a virtual machine seems complicated, but I'll look into it

    Meanwhile, would this work?
    Code:
    void getPass(string userName) {
    	string tempName, password;
    
    	ifstream userNameDB("users.data");
    
    	for (int i = 0; (userNameDB >> tempName); i = i + 2) {
    		userNameDB >> tempName;
    
    		if (tempName == userName) {
    			i++;
    			userNameDB >> password;
    			userNameDB.close();
    			logInPASS(userName, password);
    		}
    			
    	}
    	
            userNameDB.close();
    	cout << endl << "Error fetching password for " << userName << "." << endl;
    	writeError(1);
    	
    	return -1;
    }

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by PAragonxd View Post
    Setting up a virtual machine seems complicated, but I'll look into it
    Well, I imagine it might be on Linux, but on the other hand, it can't be that difficult.

    Meanwhile, would this work?
    Code:
    void getPass(string userName) {
    	string tempName, password;
    
    	ifstream userNameDB("users.data");
    
    	for (int i = 0; (userNameDB >> tempName); i = i + 2) {
    		userNameDB >> tempName;
    
    		if (tempName == userName) {
    			i++;
    			userNameDB >> password;
    			userNameDB.close();
    			logInPASS(userName, password);
    		}
    			
    	}
    	
            userNameDB.close();
    	cout << endl << "Error fetching password for " << userName << "." << endl;
    	writeError(1);
    	
    	return -1;
    }
    Nope, it will absolutely not work, because you're reading twice.
    userNameDB >> tempName; // Reads user
    userNameDB >> tempName; // Reads pass
    if (tempName == userName) // Fails
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    This program should work just fine with your Windows XP machine - there is nothing machine/OS specific in the way your code works, so why not just run it as a Windows console application. That is a 10x simpler solution than virtual machines or such, and as long as you are not depending on OS-specific functionality (that is, you are using standard C/C++ functionality), it shouldn't make any difference.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 01:02 AM
  2. Reading characters from a string?
    By ladysniper in forum C++ Programming
    Replies: 6
    Last Post: 04-08-2006, 11:45 PM
  3. Replies: 26
    Last Post: 06-15-2005, 02:38 PM
  4. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM