Thread: finding occurances in a file

  1. #1
    marf
    Guest

    finding occurances in a file

    I've tried several different methods to this project, using stdio.h and fstream.h
    All I want to do is read in a file line by line, search for an occurance of a few characters, then copy the text up to the occurance into a new buffer.

    I'm on a school computer and don't have access to the one right now with my code on it, but as soon as I can, I will post what I have. I was using strstr() to find the occurances of a few characters, but it returns a pointer to location of the characters. when trying to replace it with strncpy, I need the number of characters to copy before the occurance. Is there an opposite working function to strspn()? One where it returns the length of string1 until it finds the characters of string2?

  2. #2
    Registered User Cela's Avatar
    Join Date
    Jan 2003
    Posts
    362
    >>Is there an opposite working function to strspn()?
    Yep, strcspn() :-)
    *Cela*

  3. #3
    Me -=SoKrA=-'s Avatar
    Join Date
    Oct 2002
    Location
    Europe
    Posts
    448

    maybe...

    Not sure about this but...`
    Code:
    #include <iostream>
    #include <fstream>
    using namespace std;
    
    int main()
    {
      char ch;
      cout<<"What character do you want to find?";
      cin.get(ch); //get the char we want to find
      ifstream file("file.txt", ios::in); //open the file
      if(file.is_open())
      {
        while(!file.eof())
        {
          int counter=0;
          char temp; //temporal char
          cin.get(temp);
          if(temp == ch) //if the char matches the one we want
             counter++; //increase the counter by one
        }
      }
      else cout<<"Sorry, can't open file :(";
    
      cout<<'\n'<<ch<<" was repeated "<<counter<<" times."
     return 0;
    }
    sample app for the counting of occurances of a char in a file.
    Last edited by -=SoKrA=-; 02-19-2003 at 02:32 PM.
    SoKrA-BTS "Judge not the program I made, but the one I've yet to code"
    I say what I say, I mean what I mean.
    IDE: emacs + make + gcc and proud of it.

  4. #4
    marf
    Guest
    Here's the code I have just tried using Salem's idea and we still get no output. Any ideas?

    Code:
    #include <string.h>
    #include <stdio.h>
    
    int main(int argc, char *argv[])
    {
    	FILE *fp;
    	FILE *tmpfp;
    	char buffer[255]="";
    	char newbuff[255]="";
    
    	fp = fopen(argv[1],"r");
    	tmpfp = fopen("output.txt","w");
    	fgets(buffer,255,fp);
    	while (!feof(fp))
    	{
    		char *p = strstr(buffer, "ok");
    		int len = p - buffer;
    		strncpy(buffer,newbuff,len);
    		fputs(newbuff,tmpfp);
    		fgets(buffer,255,fp);
    	}
    	fclose(fp);
    	fclose(tmpfp);
    	return 0;
    }

  5. #5
    Registered User
    Join Date
    Feb 2003
    Posts
    60
    i got a program (with the help of some people especially Ronin) that finds the occurrances of each letter in the alphabet. Would you like me to post it?
    C++ can hurt.

  6. #6
    marf
    Guest
    scottmanc, sure post it please

  7. #7
    Registered User
    Join Date
    Feb 2003
    Posts
    3
    Here is some code that i wrote last year. Not sure if it helps any. It reads from an outside source and counts all the number of times that a letter appears.
    Code:
    #include <iostream.h>
    #include <iomanip.h>
    #include "apvector.h"
    #include <ctype.h>
    #include "apstring.h"
    #include <winsioux.h>
    #include <fstream.h>
    
    int main()
    {
    char ch; 
    int x=0, num=0;
    apvector<int>count(26,0);
    apvector<int>out(26,0);
    ifstream doc1;
    cout<<"1 or 2:  ";
    cin>>num;
    	if(num==1)
    		doc1.open("Nelson.txt");
    	if(num==2)
    		doc1.open("Aung.txt");
    	while(doc1)
    		{
    		doc1.get(ch);
    		ch=tolower(ch);
    		switch(ch)
    			{
    			case 'a':	count[0]+=1;
    						break;
    					
    			case 'b':	count[1]+=1;
    						break;
    			
    			case 'c':	count[2]+=1;
    						break;
    
    			case 'd':	count[3]+=1;
    						break;
    						
    			case 'e':	count[4]+=1;
    						break;
    						
    			case 'f':	count[5]+=1;
    						break;
    						
    			case 'g':	count[6]+=1;
    						break;
    						
    			case 'h':	count[7]+=1;
    						break;
    						
    			case 'i':	count[8]+=1;
    						break;
    
    			case 'j':	count[9]+=1;
    						break;
    						
    			case 'k':	count[10]+=1;
    						break;
    							
    			case 'l':	count[11]+=1;
    						break;
    						
    			case 'm':	count[12]+=1;
    						break;
    						
    			case 'n':	count[13]+=1;
    						break;
    						
    			case 'o':	count[14]+=1;
    						break;
    
    			case 'p':	count[15]+=1;
    						break;
    							
    			case 'q':	count[16]+=1;
    						break;
    							
    			case 'r':	count[17]+=1;
    						break;
    						
    			case 's':	count[18]+=1;
    						break;
    						
    			case 't':	count[19]+=1;
    						break;
    						
    			case 'u':	count[20]+=1;
    						break;
    			
    			case 'v':	count[21]+=1;
    						break;
    			
    			case 'w':	count[22]+=1;
    						break;
    			
    			case 'x':	count[23]+=1;
    						break;
    			
    			case 'y':	count[24]+=1;
    						break;
    			
    			case 'z':	count[25]+=1;
    						break;
    									
    			default:	break;
    			}	 
    		}
    	doc1.close();
    	for(char letter='A'; letter<='Z' ;letter++)
    		{
    		cout<<letter<<": ";
    		cout<<" "<<count[x]<<endl;
    		x++;
    		}
    return 0;
    }

  8. #8
    Registered User
    Join Date
    Feb 2003
    Posts
    60
    here is the code that ronin suggested to me so you should thank him.
    C++ can hurt.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. Batch file programming
    By year2038bug in forum Tech Board
    Replies: 10
    Last Post: 09-05-2005, 03:30 PM
  4. Hmm....help me take a look at this: File Encryptor
    By heljy in forum C Programming
    Replies: 3
    Last Post: 03-23-2002, 10:57 AM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM