Detecting Int in a String

This is a discussion on Detecting Int in a String within the C++ Programming forums, part of the General Programming Boards category; Hi, I am working on a small application, just to practise File I/O. The program creates a text document, named ...

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    50

    Detecting Int in a String

    Hi,
    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:

    Code:
    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): ";
    
    cin.get();
    cin.getline(data_file,11,'\n');
    
    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?
    I dont know half as many of you half as well as I should like, and I like less than half of you half as well as you deserve...

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    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";

    //typo
    {

    //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
    cin.get();

    //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
    cin.getline(data_file,11,'\n');

    //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
    data_stream_pointer<<data_file;

    //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:
    Code:
    ofstream fout ("myFile4.txt");
    
    if(!fout.good())
    {
       cout << "unable to open myFile4.txt.  Probably because myFile4.txt doesn't exist, but there are other reasons" << endl;
       fout.clear();
    }
    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
    Posts
    12
    To get a number from a string you need something like the following. Create a function to do it.
    Code:
    #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++)
    	{           //:        /
    		if(ch[i]<59&&ch[i]>47)
    		{
    			ch2[num2]=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
    Posts
    6,231
    >>if(ch[i]<59&&ch[i]>47)
    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.

    <<char(34)
    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, 07: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, 05:05 PM
  4. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 04:26 PM

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