Thread: Parsing file for spaces (filter)

  1. #1
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533

    Parsing file for spaces (filter)

    I am trying to write a program to parse a file and look for spaces and store them in an array
    then I want to make a new file with the format:
    [spaces number on this line] (rest of line without spaces)

    but all I want to do now is just store the values in the array

    Here is what I have here but the allocation of spaces into the array isn't working right.

    Code:
    #include <iostream.h>
    #include <fstream.h>
    #include <stdlib.h>
    int main(int arc, char* argv[])
    {
    	int count=0;
    	int spaces[256];
    	char a[256];
    	ifstream myfile;
    	myfile.open("c:\\luke\\test.txt");
    	
    	if(myfile.fail())
    	{
    		cerr<<"you fool!\n";
    		abort();
    	}
    	for(int i=0;i<=256;i++)
    		spaces[i]=0;
    	while(myfile.get(a))
    	{
    		for(count=0;count<=sizeof(a);count++)
    			if(a[count]==' '||a[count]=='\t')
    				spaces[count]++;
    	}
    	for(i=0;i<=10;i++)
    		cout<<spaces[i]<<endl;
    	return 0;
    }
    any ideas, if at all possible avoid a complete program, I want this mostly to be my own work, just a little help or idea would be nice though.
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    I'm not too sure what you are trying to do, so if I'm wildly off forgive me.

    Are you trying to count the number of spaces & tabs per line in a text file?

    If so, maybe you could create a vector to hold the space and tab count of each line.....you could read each line with getline() then use the STL algorithm count_if() to count if each char is a space or char...like;

    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <vector>
    #include <algorithm>
    
    
    bool IsSpace(const char& c){
    	return c == ' ' || c == '\t';
    }
    
    int main(){
    	
    	std::ifstream in;
    	std::vector<int> vec;
    	
    	in.open("MyFile.txt");
    	
    	if(!in.is_open()){
    		std::cout << "Error opening file";
    		return 1;
    	}
    	
    	
    	std::string str;
    	while(!in.eof()){
    		getline(in,str);
    		vec.push_back(std::count_if(str.begin(),str.end(),IsSpace));
    	}
    	
    	std::cout << "Number of lines counted " << vec.size() << std::endl;
    	
    	std::cout << "Number of spaces & tabs per line was;" << std::endl;
    	
    	std::copy(vec.begin(),vec.end(),std::ostream_iterator<int>(std::cout,"\n"));
    		
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  2. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  4. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM
  5. 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