Thread: new line

  1. #1
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273

    new line

    Hi, I have a code where I scan a file, but I want to scan the entire document, not only the first line. The code I have is:

    Code:
    ifstream c_file (place);		
    c_file.getline(info, 300);	
    c_file.close();
    where info is the information I want to scan, and place is the location of the file.

    I don't think getline will get the new line, is there a way of including a newline in scanning?

  2. #2
    Registered User
    Join Date
    Sep 2005
    Posts
    142

    hmmmmmm

    doesn't get line do exactly what it says (get a line),,, if you want to put the whole file into a buffer why don't you check out filebuf functions......................................... .....then you can output the whole thing onto the console......or do tring searches (whateva)
    WhAtHA hell Is GoInG ON

  3. #3
    60% Braindead
    Join Date
    Dec 2005
    Posts
    379
    I was advised not to use getline (Dont know why).

    Heres an alternative:
    Code:
    int loadtomemory(string IFpath) {
     #define maxN 4999
     #define maxL 100
      char memmap[5000];
     int x, line;
    
     ifstream file_in(IFpath.c_str(), ios::nocreate);
    
     if(!file_in)
      cout << "Could not open file.";
    
     for(x=0; x <= maxN; x++) {
      if(++line == maxL)
       break;
      file_in.get(memmap[x]);
      if(isrestricted(memmap[x] == 1) 
       break;
     }
    
     file_in.close();
     return 0;
    }
    
    int isrestricted(char res[]) {
     char restricted[] = "restricted"; //Put restricted characters here. Add space as needed.
     int x;
    
     for(x=0; x <= (sizeof(restricted[]) / sizeof restricted[0]); x++) //Not sure if this'll work. Could any pros correct this?
     if(res == restricted[x])
      return 1;
     
     return 0;
    }
    Some of that code might not work sence its not compiled, but you get the idea?
    Last edited by Blackroot; 01-19-2006 at 05:00 AM.
    Code:
    Error W8057 C:\\Life.cpp: Invalid number of arguments in function run(Brain *)

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Well, how about a loop. Calling getline in the loop will allow you to read through the whole file. The getline function will read a newline character from the stream but will not store it in the buffer.

    If you want to read the entire file in one go without a loop you can use this:
    Code:
    ifstream c_file(place);
    stringstream sstr;
    string info;
    
    if( c_file.is_open() )
    {
        sstr << c_file.rdbuf();  // Read entire file into stringstream
        info = sstr.str();       // Convert stringstream into string
        // Do whatever you want with the string "info".
    }
    The ifstream object's destructor will close the file for you.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to do encryption in C
    By sankarv in forum C Programming
    Replies: 33
    Last Post: 12-28-2010, 11:01 AM
  2. Reading a file line by line
    By Raskalnikov in forum C Programming
    Replies: 8
    Last Post: 03-18-2009, 11:44 PM
  3. Pointer and Polymorphism help.
    By Skyy in forum C++ Programming
    Replies: 29
    Last Post: 12-18-2008, 09:17 PM
  4. Printing Length of Input and the Limited Input
    By dnguyen1022 in forum C Programming
    Replies: 33
    Last Post: 11-29-2008, 04:13 PM
  5. Finding carriage returns (\c) in a line
    By JizJizJiz in forum C++ Programming
    Replies: 37
    Last Post: 07-19-2006, 05:44 PM