Reading Strings from a text file?

This is a discussion on Reading Strings from a text file? within the C++ Programming forums, part of the General Programming Boards category; Now I doubt you need to actually read this code (or if it is even possible). I don't really know ...

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    3

    Reading Strings from a text file?

    Now I doubt you need to actually read this code (or if it is even possible). I don't really know C++, which is apparent, but I do know Matlab so I'm attempting this mini-project in C++ for my work place. I've been trying to locate the answer for a while, but I feel like it might be a standard function.

    Basically I need to check an id from a scanning system and compare it to a list of ids to see if a person has clearance. I have it set up right now so the scanning system is ok, and the checking system is ok for only 1 id. Now I need to get it so it checks a text file for a large quantity of id numbers (all in the same form as id2).

    So essentially, how is it possible to extract a single string from a text file, compare to see if it is acceptable, and repeat this process until a match is found or the person does not have clearance? As of now I have a runtime error which closes the program and it becomes unresponsive.

    Code:
    #include <iostream>
        #include <vector>
        #include <string>
        #include <stdio.h>
        #include <string.h>
        #include <stdlib.h>
    #include <fstream>
    using namespace std;
    
    int main()
    {
     
        string id1="%130360765";
      string id2="U30360765";
      string id="aaaaaaaaaaaa";
      string id3="aaaaaaaaaaaa";
       
        cout << "Enter User ID"<<endl;
       cin >> id1;
       
       id=id1.substr(2,8);
       cout<<id<<endl;
       
       
        int x=0;
    
      ifstream in("C:\\Users\\Mark\\Desktop\\towel.txt");
    
      if(!in) {
        cout << "Cannot open input file.\n";
        return 1;
      }
    
      char str[255];
    
      while(in) {
        in.getline(str, 255);  // delim defaults to '\n'
        string id2=string(str);
        id3=id2.substr(1,8);
       cout<<id3<<endl;
    
      if(id.compare(id3)==0)
      {
                            cout<<"Patron has service"<<endl;
                            x=1;                 
      }
    
     
    }
     in.close(); 
    if (x==0)
       {
         cout<<"Patron does not have towel service"<<endl;
         }
         
    system("PAUSE");
    
      return 0;
      
    
    
    }
    Last edited by Soxcrates; 10-30-2010 at 06:14 PM.

  2. #2
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,425
    you need to use a loop like for or while and use an ifstream object to read your file, this will also give you the getline() member function to collect your string.
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    3
    So I think I pretty much have the basic idea down, but I seem to be doing something wrong still. The file closes after running everytime and I get these errors in Microsoft Visual

    Run-Time Check Failure #0 - The value of ESP was not properly saved across a function call. This is usually a result of calling a function declared with one calling convention with a function pointer declared with a different calling convention.

    Unhandled exception at 0x756f9617 in Whatwhat.exe: Microsoft C++ exception: std:ut_of_range at memory location 0x003ff74c.


    From Google it appears as though I might need to use a clear command with the ifstream?
    In any event, the program works, just not more than once in a row.

  4. #4
    Registered User
    Join Date
    Aug 2005
    Posts
    266
    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <sstream>
    
    using namespace std;
    int main()
    {
    	ifstream in("c.cpp");//file path
    	if(!in.good())
    	{
    		cout << "File may not be good" << endl;
    		return;
    	}
    
    	string id;
    	cout << "Enter id:" <<endl;
    	getline(cin,id); 
    	id = id.substr(2,8);
    
    	string line;
    	bool cleared=false;
    	while(getline(in,line))
    	{
    		string id_line = line.substr(1,8); 
    		// these are the bounds you supplied , starting at 1 .. ending 8 characters after index 1
    		if(id_line == id)
    		{
    			cleared = true;
    			break;
    		}
    	}
    	cout << (cleared ? "Patron has service" : "Patron does not have towel service") << endl;
    }
    Last edited by rodrigorules; 10-30-2010 at 03:54 PM.

  5. #5
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,425
    you need to post your code for this one, the file handling in c++ is pretty straight forward so i can only imagine you did something really wrong, are you maybe repeatedly opening the stream in a loop? before any other instance is close()..d
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  6. #6
    Registered User
    Join Date
    Aug 2005
    Posts
    266
    yep, im guessing thats not the complete code

  7. #7
    Registered User
    Join Date
    Oct 2010
    Posts
    3
    I editted in the old code on the first post if you just want to see what was wrong for your own amusement. I'm not used to the complete code required in C++ because I work without headers and that sort of deal with Matlab.

    Thanks rogster for pointing me to ifstream, that's what I was looking for.

    Thanks rodrigo for creating/modifying my garbled code into a workable form.

    I honestly appreciate the help because you guys saved me a ton of time.

    I guess I need to go back and learn the C++ basics since it's been a while. I always lose myself with my bracket usage.

  8. #8
    Registered User Swarvy's Avatar
    Join Date
    Apr 2008
    Location
    United Kingdom
    Posts
    195
    I've had a look at your code and I've noticed these two lines:
    Code:
    id = id.substr(2,8);
    and
    Code:
    string id_line = line.substr(1,8);
    Just out of curiousity, why are you 'clipping' these strings?

    This is a minor note but instead of doing this:
    Code:
    if(id_line == id)
    it may be better if you did something like this:
    Code:
    if( !id.compare(id_line) )
    Also, in your if statement you set the value of 'cleared' to true and then print out a statement based on the value of 'cleared'. Why not just print the statement from your if statement?

    Edit: I just realised that I was reading rodrigorules's code instead, but your program works in a very similar way.

    Why do you print out a statement saying a patron has towel service in the while loop but then you have this if statement afterwards which seems a bit out of place. The only way this if statement will be true is if no one has towel service because in your while loop you change the value of x to '1' if someone has it, but you don't take it back down afterwards, and even if you did set it to '0' when someone didn't have it, it wouldn't print out the message because your if statement is after the loop.
    Code:
    if (x==0)
       {
         cout<<"Patron does not have towel service"<<endl;
         }
    Also, change this line:
    Code:
    while(in)
    to:
    Code:
    while(in.good())
    I'm also not sure what the point of this is:
    Code:
    string id1="%130360765";
      string id2="U30360765";
      string id="aaaaaaaaaaaa";
      string id3="aaaaaaaaaaaa";
    Why do you initially set them to some stuff if you don't do anything with them? It really makes very little difference to the program, so maybe I'm just being pedantic. Are they relics from your initial version of the code?
    Last edited by Swarvy; 10-31-2010 at 07:06 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 05-05-2010, 02:43 PM
  2. Reading issue, mixed text file
    By hawaiian robots in forum C Programming
    Replies: 7
    Last Post: 05-22-2009, 04:38 AM
  3. Replies: 7
    Last Post: 02-02-2009, 06:27 AM
  4. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  5. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM

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