0

I am trying to read a line of text from file(example below). Every line of text has: title of the book(starts with capital letter, can be multiple words), number of the book, country origin of the book(starts with capital letter) and catalog of the book(starts with capital letter, can be multiple words). Every line ends with a "\n" and the next line starts. I want to read the line and seperate title, number, country and name. How to do it properly?

I tried to read char by char, and when char I am currently reading is either a number or a Capital letter and previous char was a "space", flag should go up to signalize I should move from for example title to number, number to country etc. When i read a "\n" all flags should go down and the process should start again

void openfile(char* filedestination)
{
FILE *file = fopen(filedestination, "r+");
int help[100];
int c;
int name[100];
char title[50];
int country[50];
int number[6];
int i = 0, flagt = 0 , flagn = 0 , flagc = 0, flagname = 0;
if (file == NULL)
{
    printf("Opening error");
    exit(-1);
}
while((c = fgetc(file))!=EOF)
{

    help[i] = c;
    if (flagt == 0) {
        for (int j = 0; j < i; j++) {
            name[j] = help[j];
            if (c > 47 && c < 91 && help[i - 1] == 32) {
                flagt = 1;
                j = 100;
            }
        }
    }
    if (flagc == 0)
    {
        int j = 0;
        while (flagc == 0) {
            number[j] = help[i];
            if (c > 47 && c < 91 && help[i - 1] == 32) {
                flagc = 1;
            }
            j++;
            i++;
        }

    }
    i++;
    printf("\n");
    for (int j = 0; j < i; j++) {

        printf("%c", name[j]);

    }
    for (int j = 0; j < i; j++) {

        printf("%c", country[j]);
    }
    fclose(file);
}
}

Example line:

Mountains 10002 France Wonders of nature

Photonic crystals 10003 Germany Science

So I should get title = Mountains, number = 10002, country = France, name = Wonders of nature. Then I will use it in another function, so I can overwrite name after I read it from the first line, because I won't need it.

8
  • And the question is? Commented Jun 5, 2019 at 7:53
  • @SergeBallesta Well, I am getting a lot of errors and don't know whether my direction is a good one, so the question is how to do this properly? Commented Jun 5, 2019 at 8:00
  • 2
    Consider "Democratic Republic of Congo Wonders of nature". Without any hard-coded context, vis-a-vis country/category nanes, how would we know where the country ends and the category begins? This is a bad data layout - tab or punctuation separation would be better. Commented Jun 5, 2019 at 8:05
  • @Mike I absolutely agree, but it's not my idea, it's the task I was given to do. Probably with the input seperated by punctuation I wouldn't have problems Commented Jun 5, 2019 at 8:14
  • You are also going to have to buffer the content since the one constant is the number which has to be the last number in case it's in the title (eg Basket Weaving 101, Slaughter House 5 etc). Think about how you would read each line into a buffer and use strchr and strspn to parse that buffer. Commented Jun 5, 2019 at 8:46

1 Answer 1

2

Well, this is not a very good question for SO, because it is lacking context on what you are really trying to do. Anyway, I have had a look at your code but there would be too many things to fix for a SO answer. So I will just give you some advices.

  • The best tool for a beginner is a pencil and a paper sheet. Write down the algo you want your program to follow, and control (yes from the paper) that is should work by following it with the initial data. If you had do that, you would have immediatly found that it contains an infinite loop starting at while (flagc == 0) {, because c is never changed in that loop. Only start coding when you really know what you want

  • You are trying to parse a file one character at a time. It is not really hard, but require a lot of attention. As a beginner, I would advise you to use as much higher level function from standard library as possible: read the file line by line with fgets and then use strcspn and strspn to find the position of the number in the line. What comes before is the title, what comes after is the name.

  • Be careful with indentation (many IDE or editors can automatically indent your code). If the file was correctly indented, you would have immediately seen that the fclose call was inside the main loop. So your current code closes the file after the first character.

  • use shorter functions. If a function becomes too large, try to split it. Here the parsing function should parse a single line and return the title, number and name. That way the high level algo becomes:

    open file
    loop line by line
        extract title, number and name from a line
        process title, number and name
    close file
    

    and then, write down the parsing algo to extract the fields from a line

  • when things go wrong (yes they will, even if you follow my advices...), use a debugger to single step through the code. The infinite loop would have been evident if you had...

  • less important, but good practices recommend to avoid magic numbers in the code. It is nice for you to know the ascii code table, but ' ' for a space is easier to read than 32. At least you immediately know that it is a char and not a true number...

It may not be the expected answer, and I could certainly code that for you, but you would not have learned anything from it.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.