Click here to Skip to main content
15,889,844 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hey,

I am having some problem in the code given below, why "line" parameter is not working?
//////////////////////////////Code//////////////////////////////////////////
C#
#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
Comments
saad_lah 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

I can see a few problems here.

1. Get rid of these lines:
C++
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:
C++
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):
C++
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
 
Share this answer
 
Comments
saad_lah 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 :(
chaau 13-Dec-12 13:57pm    
have you replaced the "<=" with "<" I have suggested in #3? It is the core of your issues.
saad_lah 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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900