Thread: Wierd buffer problem reding characters

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    30

    Wierd buffer problem reding characters

    Ok, I am just writing this simple program that reads a text file char by char but when i use the buffer variable thats a single type char, and i then ask in an if statement in the buffer variable equals another single char variable it ALWAYS equates to false. Ive tried all kinds of combinations of making the two variables pointers and refferences and nothing changes the all false, though some work without errors. I realize im passing the refference when i pass it to the get or read operations, so for some reason does that make the result unable to go through any operations??even equal.

    If I just output the chars im reading i can output a txt file just dandy. But all i want it to do is say if it equals another char or not and it never equals.

    Any ideas?
    "...son, what is this COUT << on my birthday card mean?"
    expected ";" line 12, 54, 63, 73....

  2. #2
    Registered User
    Join Date
    Mar 2003
    Posts
    30

    SORRY!!!! correction

    I am NOT passing a refferance to the infile.get(buff) sorry.
    "...son, what is this COUT << on my birthday card mean?"
    expected ";" line 12, 54, 63, 73....

  3. #3
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    code might help.

  4. #4
    Registered User
    Join Date
    Mar 2003
    Posts
    30

    Code then.

    Code:
    #include<iostream.h>
    #include<fstream.h>
    #include<string.h>
    
    
    
    void main (void)
    {
    char buffer;
    char file[256]= "C:\windows\desktop\cpptest.txt";
    int pattern_array[50];
    char current_char = 'i';
    char buffer_got;
    //current_char[1] = 'i';
    int pap;
    cout << "Input the path and file name to open:  ";
    		//modified for debug
    
    ifstream infile(file);
    pap = 0;
    while(!infile.eof() && pap < 50)  //fill patern array with 0 or 1
    	{
    	infile.get(buffer);
    	//buffer_got = buffer; tried this too
    	if(current_char == buffer)
    		{
    		pattern_array[pap] = 1;
    		}
    	else
    		{
    		pattern_array[pap] = 0;
    		};
    	pap++;
    	};
    pap = 0;
    while(pap < 50)
    	{
    	cout << pattern_array[pap];
    	pap++;
    	};
    infile.close();
    
    };
    "...son, what is this COUT << on my birthday card mean?"
    expected ";" line 12, 54, 63, 73....

  5. #5
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    try this:
    Code:
    while(infile >> buffer && pap < 50)
    you also do not need all the ; after the while loops and if statements, nor after main. you should really be using int main()

    you could also try:
    Code:
    while(infile.get(buffer) && pap < 50)
    but I'd try the first way first.

    then just have your if statements in while, an remove infile.get(buffer) from the beginning of the while.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. writing a pack-style function, any advices?
    By isaac_s in forum C Programming
    Replies: 10
    Last Post: 07-08-2006, 08:09 PM
  2. Replies: 4
    Last Post: 09-23-2005, 10:51 AM
  3. Tetris Questions
    By KneeGrow in forum Game Programming
    Replies: 19
    Last Post: 10-28-2003, 11:12 PM
  4. wierd random problem
    By bart in forum C++ Programming
    Replies: 3
    Last Post: 11-17-2001, 10:15 AM
  5. getline problem
    By Unregistered in forum C++ Programming
    Replies: 4
    Last Post: 10-06-2001, 09:28 AM