Simple fp_in question.

This is a discussion on Simple fp_in question. within the C++ Programming forums, part of the General Programming Boards category; Hello everyone! This is my first post here and I have to say that I am very glad to have ...

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    2

    Simple fp_in question.

    Hello everyone!
    This is my first post here and I have to say that I am very glad to have found this website.

    My problem, however simple it may seem to you, has baffled me for a week now, and I have scoured the Internet for an answer to no avail. I just started seriously programming things that my friends and I find useful, and have hit a road block in my latest project.

    The program is supposed to read a notepad file, line by line, and use those lines to perform an action. It's sort of an attempt to make a very simplified language for them to be able to give it some simple instructions.

    I hit my difficulties when I actually try to split up the notepad file. My problem is the fact that I have no idea how =P. I want to check each line of the .txt file one by one, and assign each of those to it's own string. Here's what I have so far (this only reads the first line)

    I've posted only the code relevent to my question, this only prints the line on screen.

    Code:
    {
        GetFileName();
        ifstream fp_in;  
        ofstream fp_out;
        fp_in.open(filename, ios::in);   
        fp_in >> mystring;
        cout << "First line is as follows: ";
        cout <<  mystring;
        fp_in.close();  
        cin.get();
    }
    So my question is simple, how can I modify this to read other lines of that same file?

    Many many thanks to those who read or help!

  2. #2
    Registered User
    Join Date
    May 2006
    Posts
    903
    Code:
    std::string tmp;
    while(std::getline(fp_in, tmp, '\n')) {
      // process tmp
    }

  3. #3
    Registered User
    Join Date
    Aug 2006
    Posts
    2
    Thanks a lot for your response, but I am sort of confused here... how do I put that in my code and where do I put the line that it's reading? I should've probably explained that I need it to be able to go to any line at any given time.

    Thanks again.
    (did I meantion that I'm still pretty... eh... stupid? O.o)

  4. #4
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,074
    Quote Originally Posted by BluePudding
    I should've probably explained that I need it to be able to go to any line at any given time.
    This is simple enough. Just store each line into an array or a vector. Make sure you use a '\n' as a delimeter in getline() as that will be the end of each line.
    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <vector>
    
    int main() {
       std::ifstream inFile("myInstructions.txt");
       std::vector<std::string> instructions;
       
       while(inFile) {
          instructions.push_back(*(new std::string));
          std::getline(inFile, instructions[instructions.size()-1], '\n');
       }
       
       std::cout << "Here are the instructions:\n";
       for(std::vector<std::string>::iterator i = instructions.begin(); 
                                              i < instructions.end(); i++) {
          std::cout << *i << '\n';
       }
       
       std::cin.get();
       
       return 0;
    }
    
    /* Output:
    Here are the instructions:
    Do homework.
    Go to sleep.
    Wake up.
    Take shower.
    Get dressed.
    Eat breakfast.
    Go to school.
    
    */
    Sent from my iPadŽ

  5. #5
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    You said you "scoured the Internet". A quick scouring should have revealed everything you needed.

    cppreference.com
    cprogramming.com/tutorial
    faq.cprogramming.com
    And many more...


    By the way, getline() delimits on '\n' by default.
    Last edited by jafet; 08-09-2006 at 08:08 AM.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  6. #6
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,074
    Quote Originally Posted by jafet
    By the way, getline() delimits on '\n' by default.
    That doesn't stop him from trying another character instead.
    Sent from my iPadŽ

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple question regarding variables
    By Flakster in forum C++ Programming
    Replies: 10
    Last Post: 05-18-2005, 08:10 PM
  2. Simple class question
    By 99atlantic in forum C++ Programming
    Replies: 6
    Last Post: 04-20-2005, 11:41 PM
  3. Simple question about pausing program
    By Noid in forum C Programming
    Replies: 14
    Last Post: 04-02-2005, 08:46 AM
  4. simple question.
    By InvariantLoop in forum Windows Programming
    Replies: 4
    Last Post: 01-31-2005, 11:15 AM
  5. simple fgets question
    By theweirdo in forum C Programming
    Replies: 7
    Last Post: 01-27-2002, 05:58 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21