Put a variable within a string & problem with looping a function that opens a file

I’m very new to programming and recently, using the little syntax I know started to write a program. To make a long story short I hit a few bumps and would greatly appreciate some help.
PROBLEM 1:
Here is an example of the piece of code im having trouble with.

int number;
number=1;

string url="http://something.com/results.aspx?cat= %22%22& sum= 1&qo=3500&eh= ";


Ok, so I want to know if and how I can put a variable inside the string url so it would have the affect of:

String url=” http://something.com/results.aspx?cat= %22%22& sum= 1&qo=”<<number<<”&eh=”;

The purpose to me is that this code is in a while loop and the variable number will be incremented every time it goes through and this leads me to my second problem.

PROBLEM 2:
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <string>
#include <cstring>
#include <windows.h>
#include <urlmon.h>

using namespace std;

void getdata(ifstream & messy_file, ofstream & filterd_file);

int main()
{
ifstream input;
ofstream output;

       do{
getdata(input, output);
      
      
       input.close( );
      
       }while(i<10);


      

       output.close();
     cout << "\n\n\nEnd of Filter.\n";

}//endmain


So I need it to loop and so far when I run it the first time it executes perfect and the second loop the input file opening fails.

I need the effect of the code to open the input file then write to the output file the info I wanted it to then their will be a new input file with the same name after the code executes once(this is where my problem is since it isnt able to open it more then once) and I need it to loop so it does this again and again etc…

I hope I was clear in my question. Any help you pros can give me would be fantastic. Like i said im a begginer so I might not understand if you dont break it down for me. Thanks
SpikeysashAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

n_fortynineCommented:
1. Do you know where exactly in your string the number should be inserted? Or does the number come after a specific term? In the first case, you can use string.substr and a stringbuff to do this quite easily. The second case requires scanning through the string and that depends on where it is that you want the number to be *removed* and the new one *inserted*.

2. Do you have a variable called "i" declared in your program? From what I see there isn't.
 You can always open and close a file using open( ) and close( ). Please state your problem more specifically and if possible post your code that you're having trouble with.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
n_fortynineCommented:
For example if you know your number starts at position 57.
string url="http://something.com/results.aspx?cat= %22%22& sum= 1&qo=3500&eh= ";
('3' is at that position).
you can find the length of that number using:
#include <sstream>

int startpos = 57; //where '3' starts
int i; //the
for(i = startpos; i < url.length() -  1 && '0' <= url[i] && url <= '9'; ++i);
int x = 23450; //the number you want to replace;
std::stringstream buff;
buff << x;
string rep;
buff >> rep;
url.replace(57, i - startpos, rep);
0
SpikeysashAuthor Commented:
i know exactly where it starts its exactly like the example. I am a novice and i dont know why but i cant get your code to compile. I wanna see if the code you wrote will work for my program, can u tell me what to change i have this

#include <windows.h>
#include <urlmon.h>
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <string>
#include <cstring>
#include <sstream>
using namespace std;



int main()
{


string url="http://something.com/results.aspx?cat= %22%22& sum= 1&qo=3500&eh= ";



int startpos = 57; //where '3' starts
int i; //the
for(i = startpos; i < url.length() -  1 && '0' <= url[i] && url <= '9'; ++i);
int x = 23450; //the number you want to replace;
std::stringstream buff;
buff << x;
string rep;
buff >> rep;
url.replace(57, i - startpos, rep);










return 0;
}

But will your code find the variable and replace it with variable plus one even if the number is large or small etc... Sorry im confused like allways with c++ :) thanks
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

n_fortynineCommented:
sorry I've a typo when I wrote the code.
>>for(i = startpos; i < url.length() -  1 && '0' <= url[i] && url <= '9'; ++i);
should be
for(i = startpos; i < url.length() -  1 && '0' <= url[i] && url[i] <= '9'; ++i);

I'll do it again with proper comments, so here goes:

#include <iostream>
#include <sstream>
using namespace std;

int main() {
   string url="http://something.com/results.aspx?cat= %22%22& sum= 1&qo=3500&eh= ";
   int startpos = 57; //where '3' starts
   int i; //the position where the number ends
   //this for loop advances i until it reaches a non-numeric character, that is where the number ends
   for(i = startpos; i < url.length() -  1 && '0' <= url[i] && url[i] <= '9'; ++i);
   int x = 23450; //the number you want to replace;
   std::stringstream buff;
   //Put x into the buffer
   buff << x;
   string rep;
   //Get it out as a string.
   buff >> rep;
   //Now I replace a segment of url from position 57. The length of segment to be replaced (the number)
   //is i - startpos. and the string to be put in there is rep (i.e. the new number).
   url.replace(57, i - startpos, rep);
   cout << url << endl;
   return 0;
}

Try this and let me know if it works for you.
0
SpikeysashAuthor Commented:
Ok cool this should work nice, once i get my function to loop il see, thanks for your help with question number 1. Can anyone help me with number 2 ?

The program does this it downloads the file and opens it. I want that to loop but it fails to open the file more then one time. Anyone can fix this for me, is it not aloud to open a file twice using the same function ? Plz help

btw your the man  n_fortynine, at least i got one step fixed. Thanks.
0
n_fortynineCommented:
So you want to open your input file multiple times but keep the output file open until the end, am I correct? What does your getdata( ) look like?
I guess your code would look like this.

void getdata(ifstream & messy_file, ofstream & filterd_file) {
   messy_file.open("badfile.txt");
   filterd_file.open("goodfile.txt");
   //processing
   messy_file.close();
}

This won't work because you're trying to open the output file each time you run this function, while you don't close it until after the loop.

so try
void getdata(ifstream & messy_file, ofstream & filterd_file) {
   messy_file.open("badfile.txt");
   //Take this out:   filterd_file.open("goodfile.txt");
   //processing
   messy_file.close();
}

and in main( )

int i = 0;
output.open("goodfile.txt");
do {
   getdata(input, ouput);
   i++;
} while(i < 10);
output.close();
0
MafaldaCommented:
There are many ways to do so.
This is a general sugestion, taken from my head without trying it ...

Open the files for input and/or output and specify binary or text file

ifstream input("myinputfile.txt");
ofstream output("myoutputfile.txt");

example for explicit output text file
ofstream output("myoutputfile.txt", ios_base::app | ios_base::out);

If files are text you can read line by line using getline()
If files are binary read using read()

You loop using

while (!input.eof())
{
  // read data here
  // if necessary write to output file
}


You use streams ... the file will be closed when the  fstream object is destructed



Using std::string

look for the locations where the dtart and end of text is located using:
find()
find_first_of()
find_last_of()
or any other

Then use substr() to get the string portion that you need

Example:

string oldstr = "ABC=HELLO=XYZ";

short i = oldstr.find_first_of("=");
string newstr = oldstr.substr(i + 1, oldstr.find_first_of("=", i + 1) - i - 1);  // for substr second parameter calculate length and not position


cout << newstr << endl;

OUTPUT: HELLO


Hope this helps ... and that I haven't commited any hasty mistakes
0
SpikeysashAuthor Commented:
Ya mafalda thanks for trying to help but that part of the program works fine. What u said would have helped a day ago but i did that a different (more tedious) way. I do appreciate you trying to help at least now for next time i know how to do it easyer. Thanks M n_fortnine im gonna try to fix my problem right now and il tell you how it goes.
0
SpikeysashAuthor Commented:
btw i never closed my out put file but i did close my input file in the loop. Gonna go read what you wrote more and work on it.
0
SpikeysashAuthor Commented:
this is basically my problem code:

void getdata(ifstream & messy_file, ofstream & filterd_file);

int main()
{

ifstream page;
ofstream info;

info.open("results.txt");
     if (info.fail( ))
         {
             cout << "Output file opening failed.\n";
             page.close( );
             exit(1);
         }

do{
getdata(page, info);

      
       page.close( );
}while(x<10);

}//end main

void getdata(ifstream & messy_file, ofstream & filterd_file)
{

//get stuff from messy file here and put it in the filtered file
messy_file.close();
}//end void


Looks right to me where do you think my problem is ?


0
SpikeysashAuthor Commented:
void getdata(ifstream & messy_file, ofstream & filterd_file)
{
           messy_file.open("Unknown.txt");
     if (messy_file.fail( ))
         {
             cout << "Input file opening failed.\n\n";
             exit(1);
         }
//get stuff from messy file here and put it in the filtered file
messy_file.close();
}//end void


void getdata is more like that, sorry.
0
n_fortynineCommented:
I don't think there is any reason to close the stream twice. You've closed messy_file inside getdata (at the end) and also in your while loop:
>>do{
>>getdata(page, info);
>>page.close( );
>>}while(x<10);
0
SpikeysashAuthor Commented:
Dude I moved some stuff around and I think i just got it to work, thanks for all your help guys !
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.