Thread: Detecting Int in a String

  1. #1
    Registered User
    Join Date
    Apr 2003

    Detecting Int in a String

    I am working on a small application, just to practise File I/O.
    The program creates a text document, named by the user, and then it adds both a string & an integer value to the file.

    Here is the code wich creates the file:

    char data_file[11]="unset";
    cout<<"\n   **First Run,\n\n   *The program needs a source file, \n\n   *Please enter source file name(max 10 char): ";
    ofstream data_stream_pointer("Agent_Data_Loc.txt");
    data_stream_pointer<<data_file; // just to be able to find 
    //the map named by the user
    strcat(data_file, ".txt");
    ofstream data_stream(data_file);
    data_stream<<"Inside 1";
    The string value of data_stream_pointer is equal to the name of data_stream(wich is set by the user). I needed this in order to let the program find the text file wich was named by the user.

    But the problem is, that I need to let the program extract an integer value FROM the file(named by the user), in this case I need to find the 1 inside data_stream. Is there any way to find and extract it? And how do I let the program detect if a file exists?

  2. #2
    Registered User
    Join Date
    Mar 2002
    It is hard to help because I'm not sure what you are trying to do. As a result I have commented what your code is doing

    //declares a string and set it to a default value
    char data_file[11]="unset";


    //user instruction
    cout<<"\n **First Run,\n\n *The program needs a source file, \n\n *Please enter source file name(max 10 char): ";

    //get first char of input from input stream, but no place to store it

    //get up to 11 more char from input stream and store them in data_file as a string. I always make sure there is room for null char by allowing one less than than declared size of the char array

    //open a stream to write to file called Agent_Data_Loc.txt in ios::trunc mode
    ofstream data_stream_pointer("Agent_Data_Loc.txt");

    //write data_file to Agent_Data_Loc.txt

    //tack the txt extension on the string in data_file
    strcat(data_file, ".txt");

    //now open stream to file name stored in data_file
    ofstream data_stream(data_file);

    //now write string to data_file.
    data_stream<<"Inside 1";

    As commented there are several bugs. First comment out the line
    cin.get(). It screws things up the way it is.

    when concatenating .txt onto whatever is in data_faile, if the length of the string in data_file is over 7 char long you will overwrite data_file and cause an error at run_time that will be very difficult to pick up. If you're lucky the program will crash on the user, rather than contaminating data, etc.

    Even correcting there errors isn't likely to get you where you want to go, though, I think. I think you might be trying to get user to input name, say myFile. Then you look up myFile in the file called Agent_Data_Loc.txt and find an value associated with that name, say 4. Then attach that value to the user input name to get myFile4. Then attach the .txt extension to get myFile4.txt. Then use myFile4.txt to associate with an output stream to write other data to file. However, a lot of this sequence is speculation. A more precise description of what you are trying to do would be helpful.

    To determine if a file exists you can use the flag nocreate like this:

    ofstream fout("myFile4.txt", ios::nocreate);

    what this means is look for a file called myFile4.txt. If it doesn't exist, don't create it, meaning fout isn't opened and can't be used. If it does exist open myfile4.txt to accept input from fout, erasing all existing data originally in the file.

    Alternatively you could try this:
    ofstream fout ("myFile4.txt");
       cout << "unable to open myFile4.txt.  Probably because myFile4.txt doesn't exist, but there are other reasons" << endl;
    You should probably look up ios::nocreate if that's the way you want to go because I think it is actually a bit more sophisticated than what I indicated.

  3. #3
    Registered User
    Join Date
    May 2003
    To get a number from a string you need something like the following. Create a function to do it.
    #include <iostream>
    using namespace std;
    int main()
     	char ch[]="Figure59@?$";
     	char ch2[10];
     	int num;
     	int num2=0;
     	for(unsigned int i=0;i<strlen(ch);i++)
    	{           //:        /
    			num2++;//if double or more digits eg.103
    	num =  atoi(ch2);      
    	cout<<"The string is "<<char(34)<<ch<<char(34)<<endl;
        cout<<"The number found in the string is "<< num<<endl;
        cout <<"Press any key to end.......";
        for(;;);//keeps dos screen in view
        return 0;

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    isdigit() is a better way to do this.

    >>num = atoi(ch2);
    atoi() expects a \0 terminated char array. You didn't apply a \0, so atoi() will run into memory not owned by variable ch2.
    Also, atoi() is not a good choice. Read the FAQ to find out why, and also how to read numbers in a more controlled manner.

    I suggest you learn how to use a " in your source correctly Hard coding values the way you've done is not good practice.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to combine these working parts??
    By transgalactic2 in forum C Programming
    Replies: 0
    Last Post: 02-01-2009, 08:19 AM
  2. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  3. My graphics library
    By stupid_mutt in forum C Programming
    Replies: 3
    Last Post: 11-26-2001, 06:05 PM
  4. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 05:26 PM