File handling, character check

This is a discussion on File handling, character check within the C++ Programming forums, part of the General Programming Boards category; Im trying to write a program that can check text files for certain characters,like numbers , letters ect, what am ...

  1. #1
    Registered User
    Join Date
    Jul 2009
    Posts
    18

    File handling, character check

    Im trying to write a program that can check text files for certain characters,like numbers , letters ect, what am I doing wrong here.
    Code:
    #include <iostream>
    #include <fstream>
    #include <cstdlib>
    using namespace std;
    
    int main()
    {
    
        ifstream one;
    
        one.open("test.txt");
    //character
        char c;
    
        while (one.good())
        {
        c=one.get();//get character
    
            //see if its equal to what we want
            if(c=="f")
            {
            cout<<"yes its here";
            }
    
    
        }
    return 0;
    }
    when I try to check for a letter I get an error saying it cant allow a comparison between pointer and integer.

    When I try to search for a number it works, as in it loads, but dosent seem to see the number in my file.

  2. #2
    Registered User
    Join Date
    Mar 2010
    Posts
    68

    ansqwe

    you have to use single quotes. those double quotes create am array or characters that is null terminated

    Use this
    Code:
    if (c =='f'){
        bla bla
    }

  3. #3
    Registered User
    Join Date
    Jul 2009
    Posts
    18

    Thumbs up

    Im having problems reading 2 or more digit numbers,like 30 and 43 , I think get is turning my charecters into asci codes thats why Im able to count spaces and commas and single digit numbers between 0 and 9.

    What I want to do is count how many times numbers within a certain range appear in my file, what should I do?

    Code:
    int main()
    {
    int i=0,l=0,k=0;
    int j[65]={0};
        ifstream one;
        
    
        one.open("test.txt");
        //character
        int c;
    
        while (one.good())
        {
        c=one.get();//get character
    
            for(i=0;i<65;i++)
            {
                if(c==i)
                {
                j[i]++;
                }
            }
        }
    return 0;
    }

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, 04:53 AM
  2. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 10:54 AM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 05:10 PM

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