我試著不用 getline() 和 split().
1. TestFile.txt
A, HELLO
B, 1
7 0,1
4 5,6,7,8,9
2. code
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <strstream>
using namespace std;
void main()
{
FILE * f;
char buffer [100];
int num;
f = fopen ("TextFile1.txt" , "r");
if (f == NULL) perror ("Error opening file");
else
{
while ( ! feof (f) )
{
if ( fgets (buffer , 100 , f) == NULL ) break;
cout << "read " << buffer;
char *p = strtok(buffer, " ,"); // ',', space
while (p)
{
if ((*p >= 'A' && *p <= 'Z') ||
(*p >= 'a' && *p <= 'z'))
{ // A-Z, a-z
cout << p << endl; // do your job
}
else if (*p >= '0' && *p <= '9')
{
strstream ss(p, strlen(p));
ss >> num;
cout << num << endl;
}
p = strtok(0, " ,");
}
}
fclose (f);
}
system("pause");
}
This entry passed through the Full-Text RSS service — if this is your content and you're reading it on someone else's site, please read the FAQ at fivefilters.org/content-only/faq.php#publishers. Five Filters recommends:
留言列表