hi ,
Can someone help me with a program if possible .. I have similar files with file name like prog1.in , prog2.in .... progr100.in which has some 47 rows of data in a txt format (almost same files ). I have another file name file1.txt having 100 rows just 0 or 1 . I want a program that reads file1.txt row by row each associated with prog1.in,prog2.in.... prog100.in and if the row in file1.txt is 1 I want no change in ".in " file to be same and if its 0 that tht row numbers( suppose x) progx.in to be modified . The file is modified something like.. I want to delete 7 rows , 1st row being the row where i find the word "UPTAKE"..

Recommended Answers

All 9 Replies

hi ,
Can someone help me with a program if possible .. I have similar files with file name like prog1.in , prog2.in .... progr100.in which has some 47 rows of data in a txt format (almost same files ). I have another file name file1.txt having 100 rows just 0 or 1 . I want a program that reads file1.txt row by row each associated with prog1.in,prog2.in.... prog100.in and if the row in file1.txt is 1 I want no change in ".in " file to be same and if its 0 that tht row numbers( suppose x) progx.in to be modified . The file is modified something like.. I want to delete 7 rows , 1st row being the row where i find the word "UPTAKE"..

That's a lot to tackle in one shot, so I won't and I would recommend that you not either. Tackle it a step at a time. You are going to have to be a lot more explicit on how the modifications are to be made and you'll have to provide the .in file before and after being modified for anyone to get a feel for it. You'll need to use an ifstream to open and read from file1.txt, which contains 100 zeroes and ones. So open file1.txt, then have a for loop that uses a counter that ranges from 1 to 100, reading in one integer from file1.txt for every run through the for-loop. If that value that is read in is 1, do nothing. If it's zero, call some function to open and modify the corresponding file.

I have some some 47 rows... but from row where i find UPTAKE i have all 7 rows same . .. in all 100 files... rest is some data .. which has nothin to do with modification ..We can assume any file.. with so many rows ..

I have some some 47 rows... but from row where i find UPTAKE i have all 7 rows same . .. in all 100 files... rest is some data .. which has nothin to do with modification ..We can assume any file.. with so many rows ..

Keep in mind, we haven't seen the files so we don't know what "UPTAKE" refers to. It's hard (for me, at least) to understand your description without seeing an input file. Regardless, the advice I gave you in the last post should get you at least started so you can post some code that at least reads from file1.txt. Again, I'd advise you to post an example of the "before" and "after". Don't post 100 files, but posting one before the modification and after would be helpful.

i cant write the whole content in file .. but here are few lines and one line has word UPTAKE i was talking about ..
Ks Nu Beta Henry SnkL1 SnkS1 SnkG1 SnkL1' SnkS1' SnkG1' SnkL0 SnkS0 SnkG0 Alfa
0 0 1 0 0 0 0 0 0 0 0 0 0 0
0 0 1 0 0 0 0 0 0 0 0 0 0 0
kTopSolute SolTop kBotSolute SolBot
-1 0 0 0
tPulse
273
*** BLOCK G: ROOT WATER UPTAKE INFORMATION *****************************
Model (0 - Feddes, 1 - S shape) cRootMax
0 0.5
P0 P2H P2L P3 r2H r2L
-10 -200 -800 -8000 0.5 0.1
POptm(1),POptm(2),...,POptm(NMat)
-25 -25
Solute Reduction
f
*** END OF INPUT FILE 'PROG.IN' ************************************


from block G line i want to delete all linees before last line

i cant write the whole content in file .. but here are few lines and one line has word UPTAKE i was talking about ..
Ks Nu Beta Henry SnkL1 SnkS1 SnkG1 SnkL1' SnkS1' SnkG1' SnkL0 SnkS0 SnkG0 Alfa
0 0 1 0 0 0 0 0 0 0 0 0 0 0
0 0 1 0 0 0 0 0 0 0 0 0 0 0
kTopSolute SolTop kBotSolute SolBot
-1 0 0 0
tPulse
273
*** BLOCK G: ROOT WATER UPTAKE INFORMATION *****************************
Model (0 - Feddes, 1 - S shape) cRootMax
0 0.5
P0 P2H P2L P3 r2H r2L
-10 -200 -800 -8000 0.5 0.1
POptm(1),POptm(2),...,POptm(NMat)
-25 -25
Solute Reduction
f
*** END OF INPUT FILE 'PROG.IN' ************************************


from block G line i want to delete all linees before last line

Set up a temporary output file (call it "temp.out").
Read in the file line by line using getline and either output that line to temp.out or not based on criteria. That criteria seems like "Have we reached the word 'UPTAKE' yet?" so you can use the find function from the string class to look for "UPTAKE". If you haven't hit that word, output the line you read to temp.out. If you have hit that word "UPTAKE", you want to delete that line, so don't output it to temp.out. If there is some string that signifies the end of the lines to delete, you'll be searching for that after you've found "UPTAKE", so if you find it, you know to stop deleting (to delete is to not output the line to the temp file). In your case, I guess it would be this line or part of this line:

*** END OF INPUT FILE 'PROG.IN' ************************************

When you've gotten to the end of the file, delete the original file and rename the temp file. See this link to a code snippet from Ancient Dragon that shows how to do this.

http://www.daniweb.com/code/snippet777.html

I understood what that program does. but i have a problem like i have to check each time in another file for 0 or 1 and then do that here how can i code for that.. i m not getting any logic in mind. I hope you will help me with that.If i can get some code to work on it !! that will be helpful !!

I understood what that program does. but i have a problem like i have to check each time in another file for 0 or 1 and then do that here how can i code for that.. i m not getting any logic in mind. I hope you will help me with that.If i can get some code to work on it !! that will be helpful !!

Here's a rough skeleton.

#include <fstream>
using namespace std;

void DeleteLines (int fileNumber);

int main()
{
     ifstream ins;
     ins.open ("file1.txt");
     if (ins.fail())
     {
         // error handling code
     }

     for (int i = 1; i <= 100; i++)
     {
          int changeFileCode;
          ins >> changeFileCode;

          // check whether changeFileCode == 0.
          //  Call DeleteLines (i) or not based on
          // that.
     }

     ins.close ();
     return 0;
}


void DeleteLines (int fileNumber)
{
     // code to open up file and delete lines
}

I just wanna know like the code is for deleting one line and if i have to delete like many lines and if i am coming everytime out of loop how should i do that ?

I just wanna know like the code is for deleting one line and if i have to delete like many lines and if i am coming everytime out of loop how should i do that ?

To "delete" a line, you do not write that line to the temporary file. Ancient Dragon's link explains how to do that. Set up a text file with the name "infile.txt" and put this it that file:

Don't delete this.
Don't delete this.
I want to delete this line
Don't delete this.
I want to delete this line
Don't delete this

Run Ancient Dragon's program and see what happens. You'll obviously have to change the filenames to match yours. The skeleton code I have given you contains a fucntion that is passed an integer. As part of that function you'll take that integer (say 20) and turn it into

prog20.in

That'll be your filename.

if i have to delete like many lines and if i am coming everytime out of loop how should i do that ?

This is too vague. Do you know how to call a function? "How should I do that?' How should you do what? There are many things you need to do. Which one are you talking about? The function call? The if-statement to see whether to call the function? Turning the integer into a filename? Using the find function? You need to now make an attempt to change my and Ancient Dragon's code into something closer to what you need, post that revised code, explain what works and what doesn't, and ask a SPECIFIC question. Don't try to do the whole thing in one shot. Do it in stages, but you should now have enough to do at least part of it and post the attempt.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.