c. Language file reading questions

Asked 2 years ago, Updated 2 years ago, 47 views

For example, in the txt file, apple banana orange one two three ...

In this way, it's separated by tabs between strings, and the number of rows or columns is variable. I'd appreciate it if you could tell me how to put them in an array in C language. ㅠ<

c

2022-09-22 21:42

2 Answers

char *word = ...;
FILE *fp = fopen(...);

fscanf(fp,"%[^\t]s",word);

First, the method of reading one word separated by Tab is as above.

I think I can do what I want if I repeatedly call the stomach. An array that needs to be stored can be a two-dimensional character array, or you can create a very large one-dimensional string pool (character array), and a pointer array that points to the beginning of each word.


2022-09-22 21:42

I think it would be a little more convenient to think about reading each row from a file and parsing each row.

C is a language that is very uncomfortable with string processing, and a common way to separate a string with a specific delimiter is to use the strtok function.

If you read the tutorial below, I think you'll see the solution. :)

http://www.tutorialspoint.com/c_standard_library/c_function_strtok.htm


2022-09-22 21:42

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.