Hi, as per the requirement i was asked to create a code that fetches each line under a particular section of a text file and store it in a variable.

#include <iostream>
#include <fstream>
#include <string.h>

using namespace std;

int main()
{
	ifstream ifs("catter.txt");
	string  line;
	char name[100];
	char cat[100]="mkdir";
	
	// read a line using getline upto a certain dliminater//
	if(getline(ifs,line,'*')) 
       {
        strcpy(name, line.c_str());
	cout << name << endl;
        system(name);
	}
	getchar();
}

and my text file content would be like this

mkdir c:\folder\new\
mkdir c:folder\old\*

out of this content the code printing the contents till the deliminater '*' but when i tried to call those lines individually to create new directory,it is calling only the first line "mkdir c:\folder\new\" and creating "new" folder in the desired location leaving the rest.so i want to read and store each line of the file using getline() fucntion.

You don't need to store them all at one. Instead, loop it:

while (getline(ifs, line))
{
    // do your thing
}

When there aren't any more lines, getline will return a condition that stops the loop.

i tried that too the problem is it fetches entire thing as a paragraph and when i tried to perform manipulations like make directory the system () fucntion performs for only first line of the file.

I'm sorry, I wasn't clear enough. The default delimiter for getline is '\n'. That is what you want. After you get the line you need to parse the parts you want to keep. There are two steps: read a line and extract a substring from the line:

while (getline(ifs, line))
{
    string::size_type end = line.find_first_of('*');
    string command = line.substr(0, end);

    cout << command <<'\n';
    system(command.c_str());
}

really thanks buddy, it works fine.
since am not gud in string operations,i coulnt understand some parts of ur code .i would appreciate u if u explain or comment the code.

Thanks a lot.

Now the problem is this snippet works fine but the fact i want the section of the file to be read not the entire file.Thats the reason am using '*' deliminater in getline(ifs,line,'*') in my code.
Although i added deliminater and printf("end"); to check the flow of the loop here in this code

while (getline(ifs, line))

      {

      string::size_type end = line.find_first_of('*');

      string command = line.substr(0, end);

       

      cout << command <<'\n';

      system(command.c_str());
    printf("END");

      }

for the text file:

mkdir c:\newfolder
mkdir c:\newfolder1
mkdir c:\newfolder2*
xxxxxxxxxxxxxxxxxx
yyyyyyyyyyyyyyyyyy

the code creates folders in first loop with the printf output "END"
and again prints the other parts xxxxxxxxxxx and yyyyyyyy that comes after the dlieminater * in the text file.

Now the problem is this snippet works fine but the fact i want the section of the file to be read not the entire file.Thats the reason am using '*' deliminater in getline(ifs,line,'*') in my code.

Oh, I get it now. I thought that the '*' was on multiple lines as a wildcard or something. To stop reading the file at '*', check to see if '*' was found and removed:

while (getline(ifs, line))
{
    // locate the first occurrence of '*' in the line
    // use string::npos if '*' is not found
    string::size_type end = line.find_first_of('*');
    // extract a substring up to the found '*'
    // end==string::npos extracts the whole line
    string command = line.substr(0, end);

    cout << command <<'\n';
    system(command.c_str());

    // stop reading the file if '*' was found
    if (end != string::npos) break;
}

thank u...dear

hi thanks friend but still i couldnt pass the code for review,am a beginner in file manipulation. now th problem is


string::size_type end = line.find_first_of('*');

string command = line.substr(0, end);

the above statement fetches substring from the starting of the text file say '0' to the size of the string 'end'.

but i want the string to be from middle of the text file(string

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.