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;
}

Recommended Answers

All 19 Replies

This is most definitely wrong.

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

Look up fscanf in your Help/man pages.

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;
}
Member Avatar for iamthwee

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

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.)

Member Avatar for iamthwee
#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.

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;
}
Member Avatar for iamthwee

May I ask what compiler you're using.

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

#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....

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:

Member Avatar for iamthwee

What does your sample text file look like?

Can you post it?

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 :)

Member Avatar for iamthwee

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.

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?

Member Avatar for iamthwee

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:

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:

Member Avatar for iamthwee

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();
}

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

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

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

Member Avatar for iamthwee

This is too weird. It works on mine.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.