User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 402,003 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,386 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Views: 2134 | Replies: 8
Reply
Join Date: Feb 2007
Posts: 2
Reputation: dalton is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
dalton dalton is offline Offline
Newbie Poster

Scanning a text file for a string

  #1  
Feb 18th, 2007
ok so im scanning this text file for the string "model=" the problem is, i need to pull the numbers after that string until it reached the end of that line, this is an easy script but for some reason its just not happening. If someone smarter than myself could tell me where the script went wrong that would be awesome. Please don't send me a script completely different, I need it in this format to work with my project. Thanks!

Here is my code
#include <fstream>
#include <iostream>
 
using namespace std;
int main()
{
 ifstream openfile;                    
//input file
 openfile.open("modelset.txt");  
//input file
 
 char bigarray[300];
//array that takes value of the whole line being scanned
 char thearray[300];
//array that takes value of big array until =="model=" or '\n'
 char str;
//character (used in thearray to go through each value of bigarray
 
 ofstream File;                     
//output file (is created)
    File.open("testmodel.txt"); 
//output file (is created)
 
    while(!openfile.eof()) 
//while input isnt at the end of the file
    {                                           
        openfile.getline(bigarray,300); 
//set the current line values to bigarray
        int i=0;
//used to go through thearray one spot at a time
 
        while (str !='\n' && thearray!="model=") 
//set all values of thearray to bigarray until its end of line or "model="
        {
              thearray[i] = bigarray[i];                   
              i++;
        }
 
        i=0;    
//set i back to 0 so thearray starts at thearray[0]
 
        if (thearray=="model=")  
//see if its =="model="
        {
           delete [] thearray; 
//clear all values of thearray (is this right?)
           int testarray=1;
//boolean used so I don't effect the value of thearray
 
           while ( testarray==1 ) 
//while my boolean is true (redundant but important)
           {
                 openfile.get(str);
//start scanning thearray again (this starts after model=)
                 thearray[i] = str;  
                 i++;
                 if (str=='\n'); 
//set my boolean false if its end of line
                 {
                    testarray = 0;
                 }
           }
 
        File << thearray << "\n"; 
//print thearray (the numbers after "model=") into my output file "File" (testmodel.txt)
        }   
 }
}

Yes i know, terrible commenting but i couldnt cmment to the right of ecah line, you get the idea. The comments are under the line that they refer to.

I've attached modelset.txt if you want to see (no i cant edit this file)
The actual modelset.txt is 5.3meg so i just sent this as an example
Last edited by dalton : Feb 18th, 2007 at 1:42 pm.
Attached Files
File Type: txt modelset.txt (412 Bytes, 3 views)
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Oct 2005
Location: Manassas, VA USA
Posts: 72
Reputation: Clinton Portis is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 5
Clinton Portis's Avatar
Clinton Portis Clinton Portis is offline Offline
Junior Poster in Training

Re: Scanning a text file for a string

  #2  
Feb 18th, 2007
I think ye' problems start here:

while (str !='\n' && thearray!="model=") 
//set all values of thearray to bigarray until its end of line or "model="
        {
              thearray[i] = bigarray[i];                   
              i++;
        }

If you were using <string> class objects, you would able to make the != comparison. However, the only way you could make a boolean comparison between a cstring thearray and a string literal "model=" would be to:

1. First, make sure that in your text file, "model=" is followed immediately by a null terminating character '\0' (using escape characters in your .txt file works best when your file is opened in binary mode)

2. Using the <cstring> function strcmp( ), you can make a direct comparison between two character arrays. The function will return 0 if there are no differences between the cstrings.

Example, lets say you called openfile.getline(bigarray,300); Assuming that the first line of the .txt file contains the "model=" string token you are looking for, bigarray would then look like this:
[m][o][d][e][l][=][\0][4][9][5][2]
Notice that the delimiting '\n' newline character has been discarded by use of the getline( ) function. Tip: you will probably want the '\n' newline character in this case in order to determine the end of your array. Either add back in the '\n' character yourself, or use get( ) instead of getline( ).

With the null-terminated "model=", we can now pass bigarray into the strcmp( ) function, and it will only access the cstring up until it hits a null termiator. Even though bigarray has a whole bunch of stuff in it, strcmp(bigarray, "model=") would still return 0 indicating that the cstrings are equal.

At this point, you have determined that the line you extracted from your text file is what you are looking for. You can now make the assumption that everything starting at bigarray[7] will contain the numbers.
Last edited by Ancient Dragon : Feb 18th, 2007 at 4:13 pm. Reason: changed last paragraph per poster's request
Reply With Quote  
Join Date: Jun 2006
Location: India
Posts: 6,811
Reputation: ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold 
Rep Power: 23
Solved Threads: 339
Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Rebellion Revamped

Re: Scanning a text file for a string

  #3  
Feb 18th, 2007
Since you are using C++ already, why not do something simple like:
  1. // Not tested
  2. #include <fstream>
  3. #include <string>
  4.  
  5. int main ()
  6. {
  7. using namespace std ;
  8.  
  9. string my_str ;
  10. const char search[] = "model=" ;
  11. size_t size = strlen (search) ;
  12. size_t pos = 0 ;
  13.  
  14. ifstream in ("modelset.txt") ;
  15. ofstream out ("output.txt") ;
  16.  
  17. while ( getline(in, my_str).good () )
  18. {
  19. if ( (pos = my_str.find (search, 0)) != string::npos )
  20. {
  21. my_str = my_str.erase (pos, size) ;
  22. out << atoi (my_str.c_str()) << '\n';
  23. }
  24. }
  25.  
  26. // close the file streams
  27. in.close () ;
  28. out.close () ;
  29.  
  30. getchar ( ) ;
  31. return 0 ;
  32. }

Modify this basic framework to suit your needs.

Oh and btw, don't PM people, its rude. If we get time, we will definately help you out.
Last edited by ~s.o.s~ : Feb 18th, 2007 at 3:12 pm.
"I don't accept change. I don't deserve to live."

"Working a real job is a win if you're lazy, greedy, or unmotivated. If you're average, you fit right in. And if you're above average, the basic terms of employment and premise of the arrangement is against your interests."
Reply With Quote  
Join Date: Aug 2005
Posts: 4,711
Reputation: iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light 
Rep Power: 17
Solved Threads: 309
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Industrious Poster

Re: Scanning a text file for a string

  #4  
Feb 18th, 2007
>Since you are using C++ already, why not do something simple like:

Prolly because his school wants him/her to use and get to know char[] arrays.


>out << atoi (my_str.c_str()) << '\n';

Don't use atoi with c++ or even c for that matter. It is best avoided. Use stringstream instead.

http://www.daniweb.com/techtalkforums/thread39691.html

Oh and I just noticed an EOF in the OPs code which in most circumstances is best avoided.
Last edited by iamthwee : Feb 18th, 2007 at 3:34 pm.
Member of: F-ugly code club

Join today don't delay!
Reply With Quote  
Join Date: Jun 2006
Location: India
Posts: 6,811
Reputation: ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold 
Rep Power: 23
Solved Threads: 339
Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Rebellion Revamped

Re: Scanning a text file for a string

  #5  
Feb 18th, 2007
Originally Posted by iamthwee View Post
>out << atoi (my_str.c_str()) << '\n';

Don't use atoi with c++ or even c for that matter. It is best avoided. Use stringstream instead.

I just gave him a dummy and trivial working solution. If I would have used string streams it would have as well completed his project.

And btw atoi is not always bad, depends on the context which it is used. See this snippet.
Last edited by ~s.o.s~ : Feb 18th, 2007 at 9:20 pm.
"I don't accept change. I don't deserve to live."

"Working a real job is a win if you're lazy, greedy, or unmotivated. If you're average, you fit right in. And if you're above average, the basic terms of employment and premise of the arrangement is against your interests."
Reply With Quote  
Join Date: Dec 2006
Posts: 1,303
Reputation: Aia is a name known to all Aia is a name known to all Aia is a name known to all Aia is a name known to all Aia is a name known to all Aia is a name known to all 
Rep Power: 9
Solved Threads: 88
Aia's Avatar
Aia Aia is offline Offline
Nearly a Posting Virtuoso

Re: Scanning a text file for a string

  #6  
Feb 18th, 2007
Originally Posted by ~s.o.s~ View Post
And btw atoi is not always bad, depends on the context which it is used. See this snippet.


I followed your link because now I'm intrigued about atoi, however is for C++ and I don't know anything about it. Do you have another link that shows why is bad to use that function in C. I have used it several times, in the past, following books examples.
"No man's life, liberty, or property is safe while the legislature is in session." ~ Mark Twain
Reply With Quote  
Join Date: Apr 2006
Location: Canada
Posts: 4,472
Reputation: John A is a glorious beacon of light John A is a glorious beacon of light John A is a glorious beacon of light John A is a glorious beacon of light John A is a glorious beacon of light John A is a glorious beacon of light 
Rep Power: 16
Solved Threads: 269
Moderator
Staff Writer
Featured Blogger
John A's Avatar
John A John A is offline Offline
Vampirical Moderator

Re: Scanning a text file for a string

  #7  
Feb 18th, 2007
Originally Posted by Aia View Post
I followed your link because now I'm intrigued about atoi, however is for C++ and I don't know anything about it. Do you have another link that shows why is bad to use that function in C. I have used it several times, in the past, following books examples.
See this excellent post by Narue:
http://www.daniweb.com/techtalkforum...tml#post191438

Another way in C for conversion is to validate the string with strcspn and then use sscanf to retrieve it (although that would be kind of pointless; better to use strtol).
tuxation.com - Linux articles, tutorials, and discussions
Reply With Quote  
Join Date: Jun 2006
Location: India
Posts: 6,811
Reputation: ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold 
Rep Power: 23
Solved Threads: 339
Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Rebellion Revamped

Re: Scanning a text file for a string

  #8  
Feb 19th, 2007
Originally Posted by Aia View Post
I followed your link because now I'm intrigued about atoi, however is for C++ and I don't know anything about it. Do you have another link that shows why is bad to use that function in C. I have used it several times, in the past, following books examples.
The way in which I used atoi in that snippet was relatively safe since the string is only to passed to atoi if it contains only digits and nothing else (I make this sure in the if condition). So the chances of junk gettting to passed to atoi are none and hence it functions properly in the given context.

I could have used stringstreams but would have been an overkill considering that I know before hand the kind of input which would be passed to the atoi. An important part of being a programmer is the proper selection of tools suiting the situation under consideration.
Last edited by ~s.o.s~ : Feb 19th, 2007 at 10:39 am.
"I don't accept change. I don't deserve to live."

"Working a real job is a win if you're lazy, greedy, or unmotivated. If you're average, you fit right in. And if you're above average, the basic terms of employment and premise of the arrangement is against your interests."
Reply With Quote  
Join Date: Aug 2005
Posts: 4,711
Reputation: iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light 
Rep Power: 17
Solved Threads: 309
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Industrious Poster

Re: Scanning a text file for a string

  #9  
Feb 19th, 2007
Originally Posted by Aia View Post
I followed your link because now I'm intrigued about atoi, however is for C++ and I don't know anything about it. Do you have another link that shows why is bad to use that function in C. I have used it several times, in the past, following books examples.


Read that link I provided to show you why atoi() is bad. There are much better options to choose from in c as briefly mentioned by joeprogrammer.
Member of: F-ugly code club

Join today don't delay!
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C++ Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 9:18 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC