Reading from a files as a string

This is a discussion on Reading from a files as a string within the C++ Programming forums, part of the General Programming Boards category; Hey there, I'm writing a chatting system, like an instant messenger, that runs over my school's shared HD (accessed by ...

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    40

    Reading from a files as a string

    Hey there,

    I'm writing a chatting system, like an instant messenger, that runs over my school's shared HD (accessed by all computers on the network). I use Ubuntu at home, and the school runs XP, so I do all my compiling and testing at school, and all the writing at home. The concept I need help with makes up a large portion of the program, and if I'm doing it wrong, I'll basically have to rewrite it... so help here would save a lot of potential trouble.

    "users.data" is the file where all the username and password information is stored (writing an encrypter for it later). Every even numbered line is a username, and every odd numbered line is a password. The entered username is passed to this function, it searches the file for that username, then saves the following line, which should be the password to the specified username, as the password. It then calls the function which handles prompting the user for the correct password. Will this code work as I need it to? Specifically the 'for' loop.
    Code:
    void getPass(string userName) {
    	string tempName, password;
    
    	ifstream userNameDB("users.data");
    
    	for (int i = 0; i != userNameDB.eof(); 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;
    }
    Thanks in advance.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Have you tried the code yourself?

    Code:
    i != userNameDB.eof()
    this will almost certainly prevent the fist iteration of the loop from working since eof() is zero and i is zero -> statement is false -> no entry into the loop. Using eof() to control a loop is wrong, and here your usage of eof() is also very wrong. [eof() does not return the number of elements or size of the file - it return false if the last read from the file was NOT a failure.]

    Instead of using eof(), you should use the "success of reading data".

    Further, if we have:
    Code:
    // users.data
    John
    xyzzy
    Tim
    foobar
    Mary
    blahblah
    What will happen if the username is Mary on the second time of the loop [assuming the above eof() problem is fixed].


    --
    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.

  3. #3
    Registered User
    Join Date
    Nov 2007
    Posts
    40
    Quote Originally Posted by matsp
    What will happen if the username is Mary on the second time of the loop [assuming the above eof() problem is fixed].
    Depends, is the first line of the file considered line 0 or line 1?

    Would it start checking the passwords as usernames?

    Also, I can't find anything about how to check the success of reading data, like you suggested. I remember Elysia had something about it in his/her sig with a link, but I never looked.
    Last edited by PAragonxd; 09-11-2008 at 02:43 AM.

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,627
    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.

  5. #5
    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;
    }

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,627
    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.

  7. #7
    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.

  8. #8
    Registered User
    Join Date
    Nov 2007
    Posts
    40
    Quote Originally Posted by matsp
    This program should work just fine with your Windows XP machine
    I run Linux, so I'd have to run Windows windowed :P.

    Quote Originally Posted by Elysia
    Nope, it will absolutely not work, because you're reading twice.
    userNameDB >> tempName; // Reads user
    So could I use the same reading as both the loop condition AND for checking against the entered username?
    Code:
    void getPass(string userName) {
    	string tempName, password;
    
    	ifstream userNameDB("users.data");
    
    	for (int i = 0; (userNameDB >> tempName); i = i + 2) {
    		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;
    
    }

  9. #9
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,627
    That was the point, so yeah :)
    But I wonder if you could run Wine on the app, as well... Maybe it would work since your program is not very complicated.
    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.

  10. #10
    Registered User
    Join Date
    Nov 2007
    Posts
    40
    Awesome, thanks.

    I've tried running simple programs in Wine, but they don't work; I assume it's because they are run in the windows console, not an included interface.

  11. #11
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,627
    Wine is supposed to run Win apps exactly as they would on Windows, GUI and all (not limited to console).
    Though that doesn't mean I know why it doesn't work.
    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.

  12. #12
    Registered User
    Join Date
    Nov 2007
    Posts
    40
    Hm, I'd work on figuring it out, but I'm going back to using Windows as soon as I can. I had to reformat and reinstall Windows due to a virus, and in reformatting I lost the drivers to my Ethernet hookup. Gateway only supplies the drivers I need for Vista, and I used XP. Either upgrading to Vista or getting a new computer.. it's about that time anyway.

  13. #13
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Depends on how you're running them through wine. wine will NOT spawn a terminal window for console-based applications. Either run the program through the terminal, or run wine's 'cmd.exe' and run your program from there.

    If it is graphical, check the wine logs or output from when you run it via a terminal, ie "wine ~/.wine/c_drive/myprogram.exe"

  14. #14
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Ok, so I got it the wrong way around with where you have Linux vs Windows. But I assume this is a console application for Windows, correct? [Because I'd expect some more experience in file handling before you start doing GUI apps - but I could be wrong].

    If so, running a linux text based app should be no different from using a console app in Windows.

    And if it is a GUI app, the code you are working on right NOW is not GUI-related, so it could easily be made into a text-mode app for the time being for testing purposes - that may take a tiny bit of time to make up the framework, but it's good practice and you'd be able to test your code without having to mess about with wine or such.

    --
    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

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