Error in file reading

Guys I made a program to split a file into 11 different files based on the word length of its content. The file I'm reading my data from contains 354986 words but my program is reading only 109582 words. All the words having special characters or numbers are skipped. In addition, many other words which simply have alphabets and nothing special about them are also skipped. I'm using g++ compiler on Kali OS. Any suggestion?
Here's my code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include<iostream>
#include<fstream>
#include<cstring>
#include<cstdio>

using namespace std;

int main()
{
	char word[30],file[10]=" .txt",name[30];
	int l,i;
	unsigned int count_proc=0,count_intake=0;
	ofstream out[12],ex;
	ifstream in;
	cout<<"\nEnter file name : ";
	cin>>name;
	in.open("wordsEn.txt");
	if(!in)
	{
		cout<<"\nInput file can't be opened";
		return 1;
	}
	for(i=0;i<=9;i++)
	{
		file[0]=i+48;
		out[i].open(file);
		if(!out[i])
		{
			cout<<"\nOutput file "<<file<<" can't be opened";
			return 1;
		}
	}
	ex.open("extra.txt");
	if(!ex)
	{
		cout<<"\nOutput file extra can't be opened";
		return 1;
	}
	while(!in.eof())
	{
		in>>word;
		count_intake++;
		cout<<"\n"<<word;
		//getchar();
		l=strlen(word);
		if(l>1 && l<12)
		{
			out[l-2]<<word;
			out[l-2]<<"\n";
			count_proc++;
		}
		else
		{
			ex<<word;
			ex<<"\n";
			count_proc++;
		}
	}
	in.close();
	for(i=2;i<=13;i++)
		out[i-2].close();
	
	cout<<"\nFile Finished";
	cout<<"\nTotal words processed to files : "<<count_proc;
	cout<<"\nTotal words processed from file : "<<count_intake;
	return 0;
}
Last edited on
Is the input file ASCII?

Why do you ask name (line 16) but never use it?

char word[30]; is not safe.
std::string word; is safe.

Lines 39-41 should read:
1
2
while ( in >> word )
{
Oh! I took That input but forgot to use it. Thanks for finding the mistake. I spent hours but failed to notice. :P
Now Its working fine.
Last edited on
Topic archived. No new replies allowed.