dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9

Thread: seekg() not working?

Threaded View

  1. #1
    Join Date
    Jul 2007
    Posts
    33

    seekg() not working?

    I don't really know what the cause of this problem is, but I've definitely encountered the same problem before.

    In the code, on line 71, where I've presumed the origin of the problem to be, there is the code "image.seekg(init); cout<<image.tellg();" The only reason that I added cout<<image.tellg(); was to verify that there is a problem here. The problem is that image.tellg() always returns -1 even though the value of init is always 19. I don't know why this problem exists: There used to be no problem with this line of code before I made a few changes elsewhere in the program. It's as though the rules of C++ change every time I make a small change to my program.

    Anyways, attached is ABC.txt, and the code for the program is as follows...

    Code:
    #include<iostream>
    #include<cstdlib>
    #include<cstring>
    #include<fstream>
    using namespace std;
    
    int w,h,maxval,start,init,begy,endy,begx,endx;
    char temp[5];
    
    void cp(int a)
         {cout<<"checkpoint "<<a<<"\n";
         system("PAUSE");}
    
    void cleartemp()
         {int a;
         for(a=0; a<5; a++)temp[a]='\0';}
    
    int main()
        {cleartemp(); //identifies width, height, and maxval values
        ifstream image("ABC.txt");
        image.seekg(0);
        image.ignore(5,' ');
        image.getline(temp,5,' ');
        w=atoi(temp);
        cleartemp();
        image.getline(temp,5,' ');
        h=atoi(temp);
        cleartemp();
        image.getline(temp,5,'\n');
        maxval=atoi(temp);
        cleartemp();
        image.seekg(1,ios::cur);
        init=image.tellg();
        int x,y,a,b;
        ofstream abc("ABCanalysis.txt");
        start=0;
        int count=0;
        do
            {begx=begy=endx=endy=0;
            for(y=(int)(start/w); y<h; y++) //establishes a vertical starting point for character analysis
                     {if(y==h-1){begy=-1; break;}
                     if(begy>0)break;
                     for(x=start; x<w; x++)
                              {image.seekg(init);
                              for(a=0; a<(y*w)+x; a++)image.ignore(5,' ');
                              image.getline(temp,5,' ');
                              if(atoi(temp)!=maxval){cleartemp(); begy=y+start; break;}}}
            if(begy==-1){cout<<begy<<" "; cp(2); break;}
            for(y=begy+1; y<h; y++) //establishes a vertical end point for character analysis
                     {if(endy>0)break;
                     for(x=start; x<w; x++)
                              {image.seekg(init);
                              for(a=0; a<(y*w)+x; a++)image.ignore(5,' ');
                              image.getline(temp,5,' ');
                              if(atoi(temp)!=maxval){cleartemp(); break;}
                              if(x==w-1 && atoi(temp)==maxval){cleartemp(); endy=y+1; break;}}}
            for(x=start; x<w; x++) //establishes a horizontal starting point for character analysis
                     {if(x==w-1){begx=-1; break;}
                     if(begx>0)break;
                     for(y=begy; y<=endy; y++)
                              {image.seekg(init);
                              for(a=0; a<(y*w)+x; a++)image.ignore(5,' ');
                              image.getline(temp,5,' ');
                              if(atoi(temp)!=maxval){cleartemp(); begx=x+start; break;}}}
            if(begx==-1)
                        {start=(endy+1)*w;
                        continue;}
            for(x=begx+1; x<w; x++) //establishes a horizontal end point for character analysis
                     {if(endx>0)break;
                     for(y=begy; y<=endy; y++)
                              {image.seekg(init); cout<<image.tellg(); //This is where the problem seems to originate
                              for(a=0; a<(y*w)+x; a++)image.ignore(5,' ');
                              image.getline(temp,5,' ');
                              if(atoi(temp)!=maxval){cleartemp(); break;}
                              if(y==endy && atoi(temp)==maxval){cleartemp(); endx=x; break;}}}
            b=0;
            for(x=begx; x<=endx; x++)
                        {image.seekg(init);
                        for(a=0; a<start+x; a++)image.ignore(5,' ');
                        for(y=begy; y<=endy; y++)
                                    {image.getline(temp,5,' ');
                                    b=b+atoi(temp);
                                    cleartemp();
                                    if(y!=endy)for(a=0; a<w; a++)image.ignore(5,' ');
                                    else break;}
                        b=1-(b/(255*(endx-begx)));
                        abc.put(' ');
                        itoa(b,temp,10);
                        abc.write(temp,strlen(temp));
                        cleartemp();}
            b=0;
            for(y=begy; y<=endy; y++)
                        {image.seekg(init);
                        for(a=0; a<start+(y*w); a++)image.ignore(5,' ');
                        for(x=begx; x<=endx; x++)
                                    {image.getline(temp,5,' ');
                                    b=b+atoi(temp);
                                    cleartemp();}
                        b=1-(b/(255*(endy-begy)));
                        abc.put(' ');
                        itoa(b,temp,10);
                        abc.write(temp,strlen(temp));
                        cleartemp();}
            start=start+(endx-begx);
            count=count+1;
            cout<<count<<"\n";}while(begy!=-1);
        abc.close();
        image.close();
        return 1;}
    Attached Files Attached Files

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)