Thread: c++ Read *.bat file as *.txt

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    30

    Question c++ Read *.bat file as *.txt

    Hello. I have made a simple c++ program that can save websites into a bat file as strings. and another function with launches the bat file to open the websites.
    (the code do alot more, but thats not important).
    But I want my program to be able to read out what websites the bat file contains, and I havent found any way of doing this.
    the important part of the Code:

    Code:
    #include <stdlib.h>
    #include <iostream>
    #include <string>
    #include <fstream>
    #include <windows.h>
    
    using namespace std;
    
    int main(int argc, char *argv[]) {
    
        while(true){
            
        string command = "";
        cout<<endl<<"Enter a command: ";
        cin>>command;
    if(command == "addweb")
    {
        fstream myfiletest("website.bat");
         ofstream myfile;
        if(!myfiletest)
        {  //if no bat file exist, I create one that starts with @echo off
        myfile.open ("webbsite.bat",fstream::app);
        myfile << "@echo off"<<endl;
        myfile.close();
        }
        string website = "";
        cout<<"please enter a new website: www.";
        cin>>website;
     
        myfile.open ("website.bat", fstream::app);
        myfile << "start www."<<website<<endl;
        myfile.close();
        cout<<"added www."<<website<<endl;
        website = "";
        
    
    }
    else if(command =="openweb")
    {
        fstream myfile("website.bat");
        if(!myfile){
        cout<<"You have not added any websites. Type addweb to do so."<<endl;
        }
        else{
     system("website.bat");
               }
    }
    else if(command == "listweb")
    {
    //I would like to show a list of the websites in the *.bat here
    }
    
    }
    
    }
    An example of the bat file:
    Code:
    @echo off
    start www.google.se
    start www.geek.com
    start www.hotmail.se
    start www.xkcd.com
    If you know how to do this I would appreciate the help.
    And if its possible I would like to list the websites without the "@echo off" showing up.


    PS: My english sucks.
    Last edited by Danne; 02-19-2011 at 08:18 AM.

  2. #2
    Registered User
    Join Date
    Feb 2011
    Posts
    2
    im not sure but i think that you can check this tut:

    YouTube - C++ Tutorial - 24 - Reading From a File


    I think it will help you

    PS: nice code xD

  3. #3
    Registered User
    Join Date
    Jan 2011
    Posts
    30
    Thank you for that link.
    I folowed the tutorial (with a few changes as I had the websites in different lines)
    and it would list:
    Code:
    @echo off
    start www.example.com
    So I needed a way to get rid of both "echo off" and "start".
    I managed to come up with a solution that probably isnt the best, but it works:
    Code:
    ifstream file;
        file.open("website.bat");
        if(!file.is_open())
        {
            cout<<"You have no websites. Type addweb to add some";
        }
        else{
       string text;
       file>>text; //this will store "@echo" in the string
       file>>text; //this will overwrite "@echo" with "off"
       file>>text; //this will overwrite it "off" with "start"
       while(file.good()){
        file>>text; //this will overwrite "start" with the website
       cout<<text<<endl; //cout the website
       file>>text; //this will store "start"
       }// loop it
        }
    Works perfectly :P

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    A word of advice: your indentation is poor.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 11-11-2010, 12:05 PM
  2. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 01:04 PM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. archive format
    By Nor in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 08-05-2003, 07:01 PM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM