Text file problem.

This is a discussion on Text file problem. within the C++ Programming forums, part of the General Programming Boards category; I was just wondering if anyone could explain what is happening in the following bit of code. I know that ...

  1. #1
    Registered User subdene's Avatar
    Join Date
    Jan 2002
    Posts
    359

    Angry Text file problem.

    I was just wondering if anyone could explain what is happening in the following bit of code. I know that the code will keep getting data from the file and outputting it to the screen @ a char @ a time, but on each itteration of the loop how does the program know which character it should be outputting next. I understand it will stop once the end of file is reached, but how is the code keeping a count of which character should be displayed?

    void main()

    ifstream InFile;
    char InChar;

    InFile.open("Test.txt");

    InFile >> InChar;

    while(!InFile.eof())
    {
    cout << InChar << endl;
    InFile >> InChar;
    }

    InFile.close();
    getchar();
    }

  2. #2
    Registered User subdene's Avatar
    Join Date
    Jan 2002
    Posts
    359
    Can someone reply on this cos I can't get my head around the problem. Please! Thanks.

  3. #3
    Registered User Paro's Avatar
    Join Date
    Feb 2002
    Posts
    160
    i always thought you use get and put for getting chars from files because...well im a noob but i think you should here:

    Code:
    #include <iostream.h>
    #include <fstream.h>
    #include <conio.h>  // you need this for getchar() right?
    
    int main() 
    {
    	ifstream InFile; 
    	char InChar; 
    
    	InFile.open("C:/Test.txt"); //you gotta have a directory don't you? i use U:/ at school, but you prolly should use C:/ 
    
    	InFile.get(InChar); 
    
    	while(! InFile.fail()) 
    	{ 
    		cout << InChar //i took away the endline because then it will output everything including endlines in your file
    		InFile.get(InChar); 
    	} 
    
    	InFile.close(); 
    	getchar(); //i never knew how to get this working but if it works for you cool
    
    	return 0;
    }

    i hoped i helped in some way, that program should be able to input an entire paragraph word for word
    Last edited by Paro; 02-20-2002 at 10:09 AM.
    Paro

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,655
    Well, in C, when you declare a *FILE, it contains a pointer called the "file position indicator", which tracks the current position in the specified filestream. This pointer is taken care of by the operating system and works the similarly for C++ programs.

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,794
    It knows what to do because the InChar variable is being used to store the characters read in from the file. The cout statement in the loop outputs the value of the InChar variable to the screen and the InFile >> InChar; statement stores the characters from the file into the variable.
    "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

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >but how is the code keeping a count of which character should be displayed?
    The variable that you use to read from the stream is a simple char, it won't hold anymore so when you print out that char and try to read another the stream will have moved ahead one char (ie. the next character in the file). If you were reading an array of 20 chars then that array would be filled and the stream would move ahead 20 characters.

    This isn't exactly how streams work, but I didn't want to confuse you too much. You can safely assume that the stream you create an instance of will handle that type of thing so there's no need to worry about the actual mechanics of it.

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

  7. #7
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,794
    The file stream moves forward each time a character is read. The character variable gets overwritten each time you read from the file. What was in the variable is lost once the next character gets loaded in. There is no "count" unless you are maybe thinking about the position in the file from which you are reading from, the position that gets incremented by one byte with each act of reading a single character from that file.
    "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. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  2. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 01:19 PM
  3. Batch file programming
    By year2038bug in forum Tech Board
    Replies: 10
    Last Post: 09-05-2005, 03:30 PM
  4. Sorting text file problem...
    By John-m in forum C Programming
    Replies: 3
    Last Post: 10-01-2002, 04:51 PM

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