find and replace

This is a discussion on find and replace within the C++ Programming forums, part of the General Programming Boards category; Iam reading in a file which has a strings like This_Is_the_string I would like to replace ever _ with a ...

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    4

    find and replace

    Iam reading in a file which has a strings like

    This_Is_the_string

    I would like to replace ever _ with a <space>. How would i go about this ?


    This is the code i have so far and attached is the file am reading in. You can view another thread with the full code Here


    Code:
    void fillTable(StockTable& p)
    {
    	FILE* StockData;
    	int tmpAmount,pos;
    	long int tmpID;
    	char tmpDes[30];
    	float tmpPrice;
    
    	StockData = fopen("stock.txt","rt");
    
    	if(!StockData)
    	 {
    		cout<<"Error opening file...";
    		exit(1);
    	 }
    
    	 cout<<"Creating table...\n";
    	 while (fscanf(StockData, "%D\t%s\t%f\t%d", &tmpID, tmpDes,&tmpPrice,&tmpAmount) == 4)
    	 {
    		pos =  hashKey(tmpID);
    		strupr(tmpDes);
    		p.list[pos].intCatNumber = tmpID;
    		strcpy(p.list[pos].strDescription, tmpDes);
    		p.list[pos].intPrice = tmpPrice;
    		p.list[pos].intQuantity = tmpAmount;
    	 }
    
    	fclose(StockData);
    }
    Attached Files Attached Files

  2. #2
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    University of Waterloo
    Posts
    1,903
    Code:
    // pseudocode
    loop through your string
        if current block=='_'
           set to space
    very simple

  3. #3
    Registered User
    Join Date
    Mar 2004
    Posts
    4
    Code:
    for (int i;i<30;i++)
    {
      if (p.list[pos].strDescription[i] == '_')
      p.list[pos].strDescription[i] = ' ';
    }

    DOH, i need some Zzzzzzz i think


    Thanks!

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,802
    Code:
    #include <algorithm>
    ...
    replace(p.list[pos].strDescription,p.list[pos].strDescription+30,'_',' ');
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to find and replace a string in a file
    By salzouby in forum C Programming
    Replies: 13
    Last Post: 09-14-2010, 08:55 AM
  2. Find and replace within a string?
    By clancyPC in forum C Programming
    Replies: 3
    Last Post: 03-20-2009, 07:28 AM
  3. please help!...find and replace
    By amy589 in forum C++ Programming
    Replies: 26
    Last Post: 10-13-2006, 06:42 PM
  4. Find and replace
    By BobDole1 in forum C++ Programming
    Replies: 1
    Last Post: 04-12-2003, 10:06 PM
  5. Find and Replace Text Function
    By mart_man00 in forum C Programming
    Replies: 45
    Last Post: 03-13-2003, 09:21 PM

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