Input file stream problems

This is a discussion on Input file stream problems within the C++ Programming forums, part of the General Programming Boards category; I have been asking for like the past two weeks how to skip to the next line when reading a ...

  1. #1
    Shadow12345
    Guest

    Input file stream problems

    I have been asking for like the past two weeks how to skip to the next line when reading a file, and so far everything everyone has said doesn't actually work, I can't find it on msdn, I can't get anyone from gamedev.net to respond, is this impossible or something? All I need is a variable to keep track of the location on the line, once it gets to 15 i need it reset to zero and i need the ifstream object to ignore everything to the end of the line so that it starts reading after the '\n' symbol.

    I have already tried using seekg('\n') and it still seems that this should work, but alas all I get inputted from the file is jibberish (stuff that looks like memory addressess)

  2. #2
    Shadow12345
    Guest
    This is what I have so far, but, of course, it doesn't work (it NEVER exits the loop)

    Code:
    void InputFile() {
    	string Read;
    bool WriteToFile;
    int Index = 0;
    
    	ifstream fin;
    	ofstream fout;
    	fin.open("Coordinates.txt");
    	fout.open("Formatted coords.txt");
    
    	if(fin.fail()) 
    		MessageBox(NULL, "File Input Initialization Failed", "FAILURE", MB_OK);
    	if(fout.fail())
    		MessageBox(NULL, "File Output Initialization Failed", "FAILURE", MB_OK);
    	
    	while(fin.good()) {
    		
    		fin >> Read;
    		WriteToFile = true;
    
    		if(Index < 15) 
    			if(Read == "(")
    				WriteToFile = false;
    			if(Read == ")")
    				WriteToFile = false;
    
    		if(WriteToFile)
    			fout << Read << endl;
    
    		if(Index == 15) {	//THIS IS THE LAST PARENTHESIS OF THE LINE
    			Index = 0;		//RESET INDEX TO ZERO
    			fin.seekg('\n');//SEEK TO THE END OF THE LINE
    		}
    		else
    			Index++;	//OTHERWISE ADVANCE INDEX
    
    		if(Index >= 16)
    			MessageBox(NULL, "GREATER THAN 16", ">16", MB_OK);
    	}
    fin.close();
    fout.close();
    }
    I am just bewildered with this because it seemed like such an easy task, but nothing I have done has worked so I'm pretty upset.

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >I have been asking for like the past two weeks how to skip to the next line when reading a file
    Um...have you tried reading the line and discarding it?

    -Prelude
    My best code is written with the delete key.

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    (2,3) (67, 89) (4876, -1) "ignore the rest of the line";
    (14, 15) (23, 51), //etc.

    Read in the data in the parenthesis and ignore the rest of the line from file;

    int first;
    int second;
    int third;
    int fourth;
    int fifth;
    int sixth;
    char dummy;//will always be ignored
    string temp;//will always be ignored

    ifstream fin("data.txt");

    fin >> dummy; fin >> first;
    while(fin)
    {
    fin >> dummy;
    fin >> second;
    fin >> dummy;
    fin >> dummy;
    fin >> third;
    fin >> dummy;
    fin >> fouth;
    fin >> dummy;
    fin >> dummy;
    fin >> fifth;
    fin >> dummy;
    fin >> sixth;
    fin >> dummy;
    getline(fin, string, '\n');
    fin >> first;
    }

  5. #5
    Shadow12345
    Guest
    Hmm using getline for this hasn't been working for me lately, I tried it again just as you set it up, and it just outputs jibberish. However it is probably a logical error on my part and so I am taking a closer look at everything ihave done before posting any more errors on this subject.

    Note:
    I don't know if you care or not, but my coords are 3d

    Okay yes you are Prelude were both correct in the idea of 'inputting the line and discarding it', or using getline. My problem was that I am stupid and deserve to die. Thank you both.
    Last edited by Shadow12345; 10-06-2002 at 05:59 PM.

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >My problem was that I am stupid and deserve to die.
    No, bugs deserve to die, not the programmer that creates them.

    -Prelude
    My best code is written with the delete key.

  7. #7
    Shadow12345
    Guest
    Unless you're Shadow, then you deserve to die!

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Unless you're Shadow, then you deserve to die!
    Only if you work under me and a void main crosses my desk.

    -Prelude
    My best code is written with the delete key.

  9. #9
    Shadow12345
    Guest
    Agreed.

    What about globals? I just love those things! (i really do too)

  10. #10
    Shadow12345
    Guest
    I CANNOT BELIEVE IT! THIS ACTUALLY WORKS! THIS ACTUALLY LOADS THE SPACESHIP! THIS IS THE COOLEST THING YOU GUYS! I CAN ACTUALLY RECOGNIZE THE SPACESHIP FROM WHAT I MADE IN THE PROGRAM, THIS WILL BE SO TOTALLY SWEET IF I CAN GET IT WORK OMG OMG OMG!!!

  11. #11
    Shadow12345
    Guest
    I CANNOT BELIEVE IT! THIS ACTUALLY WORKS! THIS ACTUALLY LOADS THE SPACESHIP! THIS IS THE COOLEST THING YOU GUYS! I CAN ACTUALLY RECOGNIZE THE SPACESHIP FROM WHAT I MADE IN THE PROGRAM, THIS IS TOTALLY SWEET OMG OMG OMG!!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problems reading input file into two arrays
    By quasigreat in forum C Programming
    Replies: 2
    Last Post: 05-18-2008, 03:53 PM
  2. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. Getting an input stream to stop creating a file
    By Stevek in forum C++ Programming
    Replies: 3
    Last Post: 03-21-2003, 03:45 PM
  5. White space problems in file input
    By cxs00u in forum C++ Programming
    Replies: 4
    Last Post: 03-20-2002, 10:06 PM

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