Thread: File input / strings help

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    9

    File input / strings help

    Could use some help i am having some hard time figuring this out. What i need to do is read in a file and then based on its file extension decide what to do i.e. ".cpp" or ".html"
    I though i could just read in the file name then the extension but was not allowed. lol So any help would be appricated here is my code so far for the file input.
    Im pretty sure this could be done with strings?

    Code:
    #include <iostream>
    #include <string>
    #include <fstream>
    using namespace std;
    char* filename;
    ...
    int main ()
    {
        ...
        
        cout <<"Please enter the name of the file" << endl; 
    	filename=new char;
    	cin >> filename;
    
    ...
    }
    Last edited by seanman13579; 03-30-2011 at 09:51 AM.

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Yes you can do it with a string, call string::compare to do it.

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Your code is a ticking time bomb, plus it's unnecessary. You don't seem to understand dynamic memory fully. I would just recommend you make a std::string, then use std::getline.
    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.

  4. #4
    Registered User
    Join Date
    Mar 2011
    Posts
    9
    Yes Elysia i realize this that was something i just threw together real fast a rough draft if you will. lol here is what it would look like

    Code:
    #include <iostream>
    #include <string>
    #include <fstream>
    using namespace std;
    
    int main ()
    {
         string str, tmp;
      std::string filename;
    
    
        cout <<"Please enter the name of the file" << endl; 
    	cin >> filename;
    
    	
    	ifstream myfile(filename.c_str());
    	while(getline(myfile, tmp))
    	{
    	str += tmp;
        }
      
      
    
    return 0;
    }
    Also thanks whiteflag the compare worked perfectly.
    Last edited by seanman13579; 03-30-2011 at 11:21 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. create fstream from raw file descriptor
    By Elkvis in forum C++ Programming
    Replies: 25
    Last Post: 02-23-2011, 08:57 AM
  2. Adding data to existing data in an input file?
    By matthayzon89 in forum C Programming
    Replies: 4
    Last Post: 11-20-2010, 11:23 PM
  3. Parsing strings from an input file
    By bhdavis1978 in forum C Programming
    Replies: 2
    Last Post: 06-01-2010, 12:32 PM
  4. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  5. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM