Thread: Parsing file for spaces (filter)

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

    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.

    #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;"c:\\luke\\test.txt");
    		cerr<<"you fool!\n";
    	for(int i=0;i<=256;i++)
    			if(a[count]==' '||a[count]=='\t')
    	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
    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 could read each line with getline() then use the STL algorithm count_if() to count if each char is a space or;

    #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;
    		std::cout << "Error opening file";
    		return 1;
    	std::string str;
    	std::cout << "Number of lines counted " << vec.size() << std::endl;
    	std::cout << "Number of spaces & tabs per line was;" << std::endl;

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. me take a look at this: File Encryptor
    By heljy in forum C Programming
    Replies: 3
    Last Post: 03-23-2002, 10:57 AM