954,496 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

counting strings

I want to open a file and count the number of strings in the first line.

Assuming that each string is delimited by spaces.

I tried to do some code but yet not successful, can anyone help?

int main(int argc, char* argv[])
{
	int count = 0;
	FILE *stream;
	char str[10];

	//open a file
	if( (stream  = fopen( "input.txt", "r" )) == NULL )
		printf( "The file 'input.txt' was not opened\n" );
	else
		printf( "The file 'input.txt' was opened\n" );

	//read the first line
	while( fscanf(stream, str) != '\n')
	{
		count++;	//count the number of strings in the line (strings delimited by space)
		cout << "\ncount: " << count;
	}
	
	return 0;
}
gampalu
Junior Poster in Training
78 posts since Mar 2006
Reputation Points: 21
Solved Threads: 0
 

This is most definitely wrong.

while( fscanf(stream, str) != '\n')


Look up fscanf in your Help/man pages.

Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

This is most definitely wrong.

while( fscanf(stream, str) != '\n')
Look up fscanf in your Help/man pages.

I get the error.

But still I can´t do what I want...
It never exits the while loop...

int main(int argc, char* argv[])
{
	int count = 0;
	FILE *stream;

	//open a file
	if( (stream  = fopen( "input.txt", "r" )) == NULL )
		printf( "The file 'input.txt' was not opened\n" );
	else
		printf( "The file 'input.txt' was opened\n" );

	//read the first line
	while( fscanf(stream, "%s") != '\n')
	{
		count++;	//count the number of strings in the line (strings delimited by space)
		cout << "\ncount: " << count;
	}
	
	return 0;
}
gampalu
Junior Poster in Training
78 posts since Mar 2006
Reputation Points: 21
Solved Threads: 0
 

Are you doing this in c++, I noticed a cout there. If so there's a function getline to help you do this?

iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
 
while( fscanf(stream, "%s") != '\n')


Keep reading up on fscanf . You need another parameter, and you need to understand the return value. (Hint: check for success, not absence of one possible failure.)

Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 
#include <iostream>
#include <string>
#include <fstream> //file i/o
#include <sstream>

using namespace std;


int main()
{
    string array;
    
    ifstream in("input.txt");
    
    
    int count = 0;
    
    while (getline(in, array, '\n'))
    {
        cout << array; 
          //string token;
       istringstream iss(array);
       while ( getline(iss, array, ' ') )
       {
          count ++;
       }  
       cout<<" <Number of words: " <<count<<">"<<endl;
     count = 0; //reset count
    }
    
   
    in. close();
    cin.get();
}


Although if your words are delimitered by more than one space it doesn't work.

iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
 
Are you doing this in c++, I noticed a cout there. If so there's a function getline to help you do this?

Yes I'm doing it in C++.

I've searched the getline function, but don´t know properly how to use it...

int main(int argc, char* argv[])
{
	int cnt = 0;
	FILE *stream;
	char* psch;

	//open a file
	if( (stream  = fopen( "input.txt", "r" )) == NULL )
		printf( "The file 'input.txt' was not opened\n" );
	else
		printf( "The file 'input.txt' was opened\n" );

	//read the first line
	istream& getline( psch, 80, delim = '\n' );

	
	return 0;
}
gampalu
Junior Poster in Training
78 posts since Mar 2006
Reputation Points: 21
Solved Threads: 0
 

May I ask what compiler you're using.

By the way, your style so far is almost completely in c. You do realise that?

iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
 
#include <iostream>
#include <string>
#include <fstream> //file i/o
#include <sstream>

using namespace std;


int main()
{
    string array;
    
    ifstream in("input.txt");
    
    
    int count = 0;
    
    while (getline(in, array, '\n'))
    {
        cout << array; 
          //string token;
       istringstream iss(array);
       while ( getline(iss, array, ' ') )
       {
          count ++;
       }  
       cout<<" <Number of words: " <<count<<">"<<endl;
     count = 0; //reset count
    }
    
   
    in. close();
    cin.get();
}

Although if your words are delimitered by more than one space it doesn't work.

Hi I run your program but it gives me 1 word when it shoul count 4....

gampalu
Junior Poster in Training
78 posts since Mar 2006
Reputation Points: 21
Solved Threads: 0
 

May I ask what compiler you're using.

By the way, your style so far is almost completely in c. You do realise that?


I'm using Visual Studio 6.0. I'm more used to C than C++ :sad:

gampalu
Junior Poster in Training
78 posts since Mar 2006
Reputation Points: 21
Solved Threads: 0
 

What does your sample text file look like?

Can you post it?

iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
 

What does your sample text file look like?

Can you post it?

I've changed the ' ' by '\t' because I have the strings delimited by tabs

Thanks :)

gampalu
Junior Poster in Training
78 posts since Mar 2006
Reputation Points: 21
Solved Threads: 0
 

I've changed the ' ' by '\t' because I have the strings delimited by tabs

Thanks :)

That's what I deduced.

So my code is not without bugs... It could probably be changed to handle multiple spaces and multiple tabs used as a delimiters, which is something you should aim for.

Your program will be more reliable then.

iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
 

That's what I deduced.

So my code is not without bugs... It could probably be changed to handle multiple spaces and multiple tabs used as a delimiters, which is something you should aim for.

Your program will be more reliable then.

You're right!

How can we do that?

gampalu
Junior Poster in Training
78 posts since Mar 2006
Reputation Points: 21
Solved Threads: 0
 

You're right!

How can we do that?

You know what I can't think of an easy way to do that, but I'm sure it's possible. :lol:

iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
 
You know what I can't think of an easy way to do that, but I'm sure it's possible. :lol:

:) I can't think of an easy way or difficult way :sad:

gampalu
Junior Poster in Training
78 posts since Mar 2006
Reputation Points: 21
Solved Threads: 0
 

I believe this should work with multiple tabs or spaces...

Try it and confirm please:

#include <iostream>
#include <string>
#include <fstream> //file i/o
#include <sstream>

using namespace std;


int main()
{
    string array;
    
    ifstream in("input.txt");
    
    
    
    int count = 0;
    while (getline(in, array, '\n'))
    {
        stringstream os(array);          
        string temp;                 //a temporary string
  
  
        cout << array;
        while (os >> temp)
       {               
         //std::cout <<temp <<std::endl;
         count++;   
       }  
        
        
        
       cout<<" <Number of words in this line: " <<count<<">"<<endl;
     count = 0; //reset count
    }
    
   
    in. close();
    cin.get();
}
iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
 

I tried with this line in the file: "12 324 34 32"

And the result was 12, when it should be 4.

gampalu
Junior Poster in Training
78 posts since Mar 2006
Reputation Points: 21
Solved Threads: 0
 

Sorry my tabs and spaces disappeared, I tried with this line:

12[1 space]324[2 tabs]34[8 spaces]32

And it outputs 12 when it should be 4

gampalu
Junior Poster in Training
78 posts since Mar 2006
Reputation Points: 21
Solved Threads: 0
 

This is too weird. It works on mine.

iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You