Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++
Hey,
 
I am having some problem in the code given below, why "line" parameter is not working?
//////////////////////////////Code//////////////////////////////////////////
#include<iostream>
#include<fstream>
using namespace std;
 
void Edit_Line(string path,int Delimer,int line,string sLine)  
 
 //Parameter "line" is giving large values"
{
    char* temp;
    temp=new char[50];
    temp[49]='\0';
    bool Loopbrk=false,isWritten=false;
    int iCounter=0,iDelimer=0,i=0,count=0;
    fstream myfile(path);
    char keystroke;
    while(myfile.good())
    {
        myfile.get(keystroke);
        if(keystroke=='\n')
            iCounter++;
        while(iCounter==line)
        {
            myfile.get(keystroke);
            if(keystroke=='$')
                iDelimer++;
            while(iDelimer==Delimer)
            {
                if(isWritten==false)
                {
                    int iCounter=0;
                    int size=sLine.length();
                    while(iCounter<=size)
                    {
                        myfile<<sLine[iCounter];
                        iCounter++;
                    }
                    isWritten=true;
                }
                myfile.get(keystroke);
                myfile<<" ";
                i++;
                if(keystroke=='$')
                {
                    Loopbrk=true;
                    break;
                }
            }
            if(Loopbrk==true)
                break;
        }
        if(Loopbrk==true)
                break;
    }
    myfile.close();
    delete []temp;
}
void main()
{
    Edit_Line("student.txt",2,4,"Hello");
}
 
///////////////////////////student.txt//////////////////////////
l114443$Talha Jamil$samsung$12$17$2004$UnderGraduate$!
l114445$Kamran Javed$jannat123$12$12$2009$UnderGraduate$!
l114093$Imran Khan$pluto123$11$4$2009$UnderGraduate$!
l114444$Jamil Ahmed$origin123$11$12$2009$UnderGraduate$!
l114606$Zain Malik$samsung$9$16$2009$Graduate$!
l114441$Saad Kamran$samsung$11$4$2004$Graduate$!
Posted 12-Dec-12 6:44am
saad_lah1.1K
Comments
saad_lah at 12-Dec-12 11:49am
   
While Debugging the problem is in
temp=new char[50];
temp[49]='\0';
 
after these lines parameter "line" acquire some garbage value, something like dynamic memory is erasing parameter "line" value

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

I can see a few problems here.
 
1. Get rid of these lines:
    char* temp;
    temp=new char[50];
    temp[49]='\0';
    delete []temp;
Explanation: variable temp is not used anywhere in the code
 
2. Rename iCounter to a different name in this chunk of code:
                    int iCounter=0;
                    int size=sLine.length();
                    while(iCounter<=size)
                    {
                        myfile<<sLine[iCounter];
                        iCounter++;
                    }
Explanation: the same variable is used in the main loop to count the number of lines
 
3. In the loop above (see #2), you need to change (provided you have renamed the variable to iCounter1):
while(iCounter1<size)>
Explanation: You are accessing past the address of the sLine string.
 
4. Just a cosmetic change: get rid of variable i. It is not used.
 
Other than that, I believe the program should work. Did not test it
  Permalink  
Comments
saad_lah at 13-Dec-12 8:50am
   
I have tried alot to solve it by various methods like removing temp , changing the name of variables and some others also. but the program again and again gives garbage value which keeps on changing...i dont know what is the thing which is messing with code :(
Andrew Cherednik at 13-Dec-12 13:57pm
   
have you replaced the "<=" with "<" I have suggested in #3? It is the core of your issues.
saad_lah at 13-Dec-12 14:00pm
   
Problem solved by making a new project and compiling code, it was some debbugger bug. Well thanks for helping :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 365
1 Sergey Alexandrovich Kryukov 329
2 CPallini 270
3 Afzaal Ahmad Zeeshan 204
4 DamithSL 194
0 OriginalGriff 5,515
1 DamithSL 4,451
2 Maciej Los 3,902
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,175


Advertise | Privacy | Mobile
Web02 | 2.8.141216.1 | Last Updated 12 Dec 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100