Solved

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

Posted on 2003-11-15
13
307 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
  • 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
 
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

744 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now