read from text file

Discussion in 'C++' started by wasiy102, Aug 16, 2013.

  1. wasiy102

    wasiy102 New Member

    Joined:
    Jul 14, 2013
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    Hi

    mY files paths are defined as :



    Code:
    //sbase = 'D:\data\[B]sample_AMC[/B]\fasta_files\';
    sbase2 = 'D:\data\[B]sample_AMC[/B]\fasta_files\results\';
    snameprefix = 'orig_ind';
    snameprefix3 = 'results_ind';
    
    ...
    const string filname = sbase + snameprefix  + snamesuffix;
    const string resultsname_ = sbase2 + snameprefix3  + snamesuffix;
    
    This concatenation works perfectly fine when i have to manually defines the samplesnames


    However now I have 98 such samples names to work with.This implies I have to keep updating my sbase and sbase2 98 times.

    If my sample name changes from sample_AMC to sampleABC_123 then I have to update the path manuallyto

    Code:
    sbase = 'D:\data\sampleABC_123\fasta_files\';
    sbase2 = 'D:\data\sampleABC_123\fasta_files\results\';


    In order to make it easier,I saved all my 98 samplename entries in a text file.
    Is there a way I could read string entries line by line from a text file using c++ and concatenate it to sbase and sbase2?

    Text file looks like this
    sample_AMC
    sampleABC_123
    ..
     
  2. hobbyist

    hobbyist New Member

    Joined:
    Jan 7, 2012
    Messages:
    141
    Likes Received:
    0
    Trophy Points:
    0
    is something like below what you're after?
    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    
    int main()
    {
    	std::string sbase1, sbase2, fbuf;
    	std::ifstream infile("input.txt");
    
    	if(infile.is_open())
    	{
    		while(std::getline(infile, fbuf))
    		{
    		    sbase1.clear();
                        sbase2.clear();
    
                        sbase1 = "d:\\data\\" + fbuf + "\\fasta_files\\";
                        sbase2 = "d:\\data\\" + fbuf + "\\fasta_files\\results\\";
    
                        std::cout << sbase1
                                  << std::endl
                                  << sbase2
                                  << std::endl << "\n";
    		}
    
    		infile.close();
    	}
    }
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice