Solved

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

Posted on 2003-11-15
13
322 Views
Last Modified: 2010-04-01
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
0
Comment
Question by:Spikeysash
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 5
13 Comments
 
LVL 4

Accepted Solution

by:
n_fortynine earned 125 total points
ID: 9757876
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
 
LVL 4

Expert Comment

by:n_fortynine
ID: 9757914
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
 

Author Comment

by:Spikeysash
ID: 9758966
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 4

Expert Comment

by:n_fortynine
ID: 9759806
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
 

Author Comment

by:Spikeysash
ID: 9760091
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
 
LVL 4

Expert Comment

by:n_fortynine
ID: 9760127
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
 
LVL 6

Expert Comment

by:Mafalda
ID: 9760183
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
 

Author Comment

by:Spikeysash
ID: 9760222
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
 

Author Comment

by:Spikeysash
ID: 9760229
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
 

Author Comment

by:Spikeysash
ID: 9760301
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
 

Author Comment

by:Spikeysash
ID: 9760305
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
 
LVL 4

Expert Comment

by:n_fortynine
ID: 9760327
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
 

Author Comment

by:Spikeysash
ID: 9760342
Dude I moved some stuff around and I think i just got it to work, thanks for all your help guys !
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question