1

My c++ code looks like:

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main()
{
    ifstream fin("input.txt");
    ofstream fout("out.txt");

    string line;
    unsigned int number=0;
    int counter=0;
    while(fin>>line)
    {
        while(counter<=2)
        {
            if(line[number]=='/')
                counter++;
            number++;
        }
        for(int i=0;i<number;i++)
        {
            fout.put(line[i]);
        }
        fout.put('\n');
        number=0;
        counter=0;
        cin.clear();
    }

    cout<<"DONE!";
}

when i try to run it the program stops working, what might cause this problem? There is no infinit loop, because there is lot of '/' symbols in input.txt. The Program outputs file, this file does not contain whole information but only part of result...

If any information is needet to solve this problem, will be happy to share it.

SAMPLE of input.txt :

http://www.ttsgs.com/page/51/
http://meshing.it/companies/61855-Granify
http://www.theglobeandmail.com/report-on-business/small.....
https://venngage.com/blog/index.php/page/5/
http://www.klasscapital.com/portfolio/granify
http://content.granify.com/why-ab-testing-is-not-enough
http://meetups.shopify.com/meetups/edmonton-shopify-meet-up
http://www.klasscapital.com/partners/jeff-lawrence
https://medium.com/startup-communities/81bb8f8ddfcb
http://freshit.net/blog/internet-marketing/chyortova-dy.....
http://www.higeek.cn/granify?????????.....
http://savepearlharbor.com/?paged=2557
http://www.sellerforum.de/small-talk-allgemeines-f1/irc.....
https://trango.co/preventing-abandoned-carts-using-ai/
http://www.imdevice.com/204602/
http://www.ifanr.com/news/page/17
http://www.webdesign-inspiration.com/web-designs/style/.....
http://worthyofnote.co.uk/tag/ecommerce/page/3/
http://www.siliconsolutions-inc.com/granify-raises-1-5-.....
http://crowdfundingnews.com/category/tech/page/425/
http://meetups.shopify.com/meetups/30

SAMPLE of out.txt:

http://www.ttsgs.com/
http://meshing.it/
http://www.theglobeandmail.com/
https://venngage.com/
http://www.klasscapital.com/
http://content.granify.com/
http://meetups.shopify.com/
http://www.klasscapital.com/
https://medium.com/
http://freshit.net/
http://www.higeek.cn/
http://savepearlharbor.com/
http://www.sellerforum.de/
https://trango.co/
http://www.imdevice.com/
http://www.ifanr.com/
http://www.webdesign-inspiration.com/
http://worthyofnote.co.uk/
http://www.siliconsolutions-inc.com/
http://crowdfundingnews.com/
http://meetups.shopify.com/
https://angel.co/
http://cdling.com/
http://www.sunwei.asia/
https://angel.co/
M.M
  • 138,810
  • 21
  • 208
  • 365
rolands_usans
  • 35
  • 2
  • 9
  • 1
    Nowhere in your code do you actually check the length of the string you read in. You just assume it has at least 2 characters. – PaulMcKenzie Apr 28 '14 at 01:15
  • That does not solve the problem :( – rolands_usans Apr 28 '14 at 01:32
  • I don't have problem with the code and the sample input file. What platform are you running this on? – R Sahu Apr 28 '14 at 02:22
  • Change `line[number]` to `line.at(number)`, and `line[i]` to `line.at(i)`. This will make your program fail a bit more gracefully on a bounds error. There's nothing else that could cause this program to crash – M.M Apr 28 '14 at 04:32

2 Answers2

1

I'd re-structure the code a bit. First and foremost, I'd separate the code to read and write data from the code to trim the string where needed. Second, I'd use standard algorithms to handle most of the file I/O.

The code would look something like this:

#include <string>
#include <algorithm>
#include <vector>
#include <fstream>

struct trim {
    std::string operator()(std::string const &input) { 
        unsigned pos = 0;
        for (int i=0; i<3; i++)
            pos = input.find('/', pos+1);
        return std::string(input, 0, pos+1);
    }
};

int main() {
    std::ifstream in("input.txt");
    std::ofstream out("output.txt");

    std::transform(std::istream_iterator<std::string>(in),
        std::istream_iterator<std::string>(),
        std::ostream_iterator<std::string>(out, "\n"),
        trim());
}

Note that this depends on the fact that a URL isn't supposed to include any white space. If there's a chance that your input does contain whitespace other than the new-line characters separating the lines, then you'd also want to look at the answers to a previous question about how to iterate a line at a time. Although written specifically about std::cin, the same principles apply to essentially any input stream.

Anyway, for your sample input, this code produces the following output:

http://www.ttsgs.com/
http://meshing.it/
http://www.theglobeandmail.com/
https://venngage.com/
http://www.klasscapital.com/
http://content.granify.com/
http://meetups.shopify.com/
http://www.klasscapital.com/
https://medium.com/
http://freshit.net/
http://www.higeek.cn/
http://savepearlharbor.com/
http://www.sellerforum.de/
https://trango.co/
http://www.imdevice.com/
http://www.ifanr.com/
http://www.webdesign-inspiration.com/
http://worthyofnote.co.uk/
http://www.siliconsolutions-inc.com/
http://crowdfundingnews.com/
http://meetups.shopify.com/
Community
  • 1
  • 1
Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
-2

The expression:

fin>>line

within the while condition may not return false since ifstream::operator>> return ifstream& and ifstream test may return true if at least one of failbit or badbit is set (I don't think it's the case), then you should have an infinite loop.

lgabriellp
  • 82
  • 2
  • 5
  • 1
    what do you suggest writing into while statement when working with ifstream? [in this case `while(!fin.eof())` is not the best choise] – rolands_usans Apr 28 '14 at 01:47
  • I suggest you to test while (fin.good()), it will show if any bad state is set. Then you will be sure you can continue reading the file. – lgabriellp Apr 28 '14 at 02:01
  • 2
    @lgabriellp: That's nearly the worst advice humanly possible. You're also just dead wrong with the notion that `fin>>line` "will never return false". – Jerry Coffin Apr 28 '14 at 02:04
  • Sorry, @JerryCoffin, you are right. It will return false if an error read error (badbit set) or an logic error (failbit set) occored. I meant that in this situation, the wrong use of the ifstream API caused the error, not the read or logic erros. I will edit my answer. – lgabriellp Apr 28 '14 at 02:38
  • 1
    @lgabriellp: although your answer isn't quite as wrong now, it's still basically wrong. In fact, `while (fin>>line)` is a perfectly acceptable way to do things and `while (fin.good())` will normally cause a problem. He may have some problems, but this isn't the source. – Jerry Coffin Apr 28 '14 at 02:46
  • Could you show me any reference that while(fin.good()) will normally cause a problem? – lgabriellp Apr 28 '14 at 03:13
  • @lgabriellp: I can do even better than that. I can give an example: http://stackoverflow.com/q/4219678/179910. – Jerry Coffin Apr 28 '14 at 04:21