I've added a library for isdigit
function and changed the way that you're reading the file. You can find my code explanations where the comment starts with <<------
#include<stdio.h>
#include<iostream>
#include<string>
#include<sstream>
#include<stdlib.h> // atoi -> Convert string to int number
#include<fstream> // need for read file
#include<cctype> // <<------ Included this for isdigit
using namespace std;
int main() {
string tenfile;
cout << "Ten file: ";
cin >> tenfile;
cout << tenfile << endl; // <<------ Removed .c_str() you don't need it
int i, j;
int value;
ifstream myfile(tenfile); // <<------ Removed .c_str() you don't need it
char c;
while (myfile.get(c)) // <<------ Reading file char by char
{
//getline(myfile, data, ','); // use ' ' not " "
if (isdigit(c)) // <<------ If char is a digit
cout << "data: " << c << endl; // <<------ Print that char
}
myfile.close();
return 0;
}
This was the result of the code above:
data: 1
data: 3
data: 5
data: 7
data: 9
data: 8
data: 1
data: 2
data: 3
data: 0
data: 2
data: 4
data: 6
data: 8
data: 8
data: 1
data: 2
data: 3
data: 1
data: 3
data: 5
data: 7
data: 9
data: 8
data: 1
data: 2
data: 3
data: 0
data: 2
data: 4
data: 6
data: 8
data: 8
data: 1
data: 2
data: 3
If you don't want to add a new library just for isdigit
function, you can write it yourself. Basically this:
int check_digit (char c) {
if ((c>='0') && (c<='9'))
return 1;
return 0;
}
Afterwards you can do check_digit(c)
instead of isdigit(c)
and you can remove #include<cctype>
.
Version which can read any number of size:
#include<iostream>
#include<string>
#include<fstream> // need for read file
#include <locale>
using namespace std;
int count_words(const char*);
int main() {
string tenfile;
cout << "Ten file: ";
cin >> tenfile;
cout << tenfile << endl; // <<------ Removed .c_str() you don't need it
ifstream myfile(tenfile); // <<------ Removed .c_str() you don't need it
unsigned int i;
string full_text;
getline(myfile, full_text, static_cast<char>(myfile.eof()));
for (i = 0; i < full_text.size(); i++)
{
if (full_text[i] == '\n')
full_text[i] = ' ';
if (full_text[i] == ',')
full_text[i] = ' ';
}
const unsigned int word_count = count_words(full_text.c_str());
unsigned int k = 0;
string display_text;
for (i = 0; i < word_count; i++)
{
while (full_text[k] != ' ')
{
display_text += full_text[k];
k++;
if(k > full_text.size())
{
k = full_text.size();
break;
}
}
if (full_text[k] == ' ')
k++;
cout << "data: " << display_text << "\n";
display_text.clear();
}
myfile.close();
return 0;
}
int count_words(const char* str)
{
bool in_spaces = true;
int num_words = 0;
while (*str != NULL)
{
if (*str == ' ')
{
in_spaces = true;
}
else if (in_spaces)
{
num_words++;
in_spaces = false;
}
++str;
}
return num_words;
}
Input file:
10,3,5,7,9,8,1,2,3
0,2,4,6,198,8,1,2,3
1,3,5,7,9,8,1,2,3
0,2,4,6,8,8,1,2,3000
Output:
Ten file: binStr.txt
binStr.txt
data: 10
data: 3
data: 5
data: 7
data: 9
data: 8
data: 1
data: 2
data: 3
data: 0
data: 2
data: 4
data: 6
data: 198
data: 8
data: 1
data: 2
data: 3
data: 1
data: 3
data: 5
data: 7
data: 9
data: 8
data: 1
data: 2
data: 3
data: 0
data: 2
data: 4
data: 6
data: 8
data: 8
data: 1
data: 2
data: 3000