0

Actually I am trying to solve a problem using C file management where I have to read a text file where are some data given of multiple student. The problem is with their name. I thought I can solve that problem with first name and last name but then I figured out that some of name are multiple word more than two. So is their any way to solve that full name issue?

The text file is in txt format.

1. 1330273 Sajjad Kashem 1 0 1 0 0 1 1 1 0
2. 1520297 A. S. M. Irfan 0 0 0 1 1 0 1 1 1
  • 2
    Can you post what you've tried already? – possum May 02 '20 at 10:38
  • From the sample data in your question, it looks like each line in the text file has the same structure, namely a number followed by a dot followed by a single space followed by a number followed by a single space followed by the full name followed by a space and finally a series of nine binary digits where each digit is separated by a single space. Seems simple enough to do with function `fscanf()` – Abra May 02 '20 at 10:45

1 Answers1

2

If the next character after name is 0 or 1, you could try this approach:

sscanf(line, "%d. %d %[^01]%d %d %d %d %d %d %d %d %d", &a, &b, name, &c, &d, &e, &f ...);

Don't forget to strip name: last character can be space.

nik7
  • 806
  • 3
  • 12
  • 20
  • but it stops the loop and show only one student's data, i mean same data. like `1. 1330273 Sajjad Kashem 1 0 1 0 0 1 1 1 0 1. 1330273 Sajjad Kashem 1 0 1 0 0 1 1 1 0` – Zahirul Islam Nahid May 02 '20 at 11:20
  • 1
    Yes, it's a problem. You can read file line by line like it is described [here](https://stackoverflow.com/a/44862617/11914501) and then use `sscanf` instead of `fscanf` with the similar signature. – nik7 May 02 '20 at 11:24