Home > Back-end >  How can the c language statistics how many Spaces, every row in the file and keep only the first row
How can the c language statistics how many Spaces, every row in the file and keep only the first row

Time:10-14

C language classes have encountered problem, need to do a word learning system, I think the first step is to use fscanf reads TXT file to structure to words and Chinese meanings in the array, the file is each word and its meaning in Chinese a line together, there is a space between the words and meaning, but some words is there are two different parts of speech in Chinese meaning, so there are two Spaces between a line, I can't to save both Chinese and English respectively when using the fscanf structure, also can't undertake after query and learning functions,
Want to one night all don't know what to do, file has nearly 4000 words, if one will be the one in the file space delete it too time-consuming, want to ask is there any great god has a solution,

CodePudding user response:

?? Put the wrong picture, the first picture is wrong

CodePudding user response:

save the children

CodePudding user response:

Each read a line and then find the first position of the space, the space into a '\ 0', this line is divided into two parts

CodePudding user response:

The regular expression, can do,

CodePudding user response:

reference akari10032 reply: 3/f
each read a line, then find the first position of the space, the space into a '\ 0', this line is divided into two parts

Is there a way to read every time a line

CodePudding user response:

reference 4 floor proorck2019 response:
regular expression, can complete,

Ah? How to use a regular expression?..

CodePudding user response:

Use of fscanf can, but need three % s (fscanf (" % s % s % s ", the word, means [1], means [2]);)

In addition, can also pass the fgets reads the first line, and then separated by strtok_r/strtok,

Each word to create a structure, a word, the two means

 struct words_msg {
Char word [16].
Char means [2] [32].
};

CodePudding user response:

reference 7 building self-confidence boy reply:
with fscanf can, but need to three % s (fscanf (" % s % s % s ", a word that means [1], means [2]);)

In addition, can also pass the fgets reads the first line, and then separated by strtok_r/strtok,

Each word to create a structure, a word, the two means

 struct words_msg {
Char word [16].
Char means [2] [32].
};

But some words no two kinds of meaning, read will not go wrong

CodePudding user response:

reference 5 floor giarffe2333 reply:
Quote: refer to the third floor akari10032 response:

Each read a line and then find the first position of the space, the space into a '\ 0', this line is divided into two parts

Is there any way each read a line

The fgets () function every time read a line, then STRCHR () to find the location of the first space appears, will this position as' \ 0 'and recorded an address, such a line is divided into two parts of two words and definitions

CodePudding user response:

refer to the eighth floor giarffe2333 response:
Quote: reference 7 building self-confidence boy reply:

Use of fscanf can, but need three % s (fscanf (" % s % s % s ", the word, means [1], means [2]);)

In addition, can also pass the fgets reads the first line, and then separated by strtok_r/strtok,

Each word to create a structure, a word, the two means

 struct words_msg {
Char word [16].
Char means [2] [32].
};

But some words no two kinds of meaning, read won't go wrong when you

Try the second method, the strtok
  • Related