Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6979773
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T17:55:43+00:00 2026-05-27T17:55:43+00:00

I have a function that reads a formatted file. It looks like this: 1;Name_of_the_author;The

  • 0

I have a function that reads a formatted file. It looks like this:

1;Name_of_the_author;The date when the quote was published;The author of the quote;The quote
2;Name_of_the_author_2;The date when the second quote was published;The author of the second quote;The second quote

So, the delimiter is ; . What I have to do is to check every sequence/token and to check if it’s correct. The problem however is that it doesn’t get all the tokens, just the first three, after the date it just breaks, it doesn’t move through… here’s the attached code function. Ignore the comments, it’s for a school project and the comments are in romanian.

int svnCheckDb()
{
    FILE *file;
    int k, p, i=2, m, j=0;
    char mystring[1000000], *var, *var2, *string;
    file = fopen("db.txt", "r"); //deschidem fisierul
    if(file == NULL) {
        return 0;
    }
    else {
         //il putem accesa.
        while(fgets(mystring, 1000000, file) ) {
            if(j != 0)
            {
                //nu luam si prima linie cu descrierea repo-ului, prelucram doar citatele, j-ul numara randul pe care suntem
                //separam cu strtok linia citita si verificam fiecare informatie in parte pentru a fi corecta
                var = strtok(mystring, ";");
                k=1;
                /*
                k numara string-urile citite din descrierea citatelor tocmai citita. Primul e numarul de ordine, al doilea e utilizatorul
                care a adaugat citatul, al treilea reprezinta data adaugarii citatului, dupa care urmeaza citatul.
                */
                while(var != NULL) {
                    printf("k is %d and var is %s \n", k, var);
                    switch(k)
                    {
                        case 1:
                           //numarul de ordine. Daca e 0, inseamna ca nu e numar, returnam false
                            i = atoi(var);
                            if(i == 0)
                                return 0;
                            break;
                        case 2:
                            //utilizatorul care a adaugat citatul. Daca e gol sau nu e format doar din caractere a-z A-Z, returnam false
                            for( m = 0; m < strlen(var); m++ )
                                if(!isalpha(var[m]))
                                   return 0;
                            break;
                        case 3:
                            //data la care a fost adaugat citatul. Intrucat folosim formatul DD MM YY cu spatii intre ele, vom verifica daca e ok in fisier
                            string = var;
                            var2 = strtok(string, " ");
                            p=1; //folosim p sa vedem daca am ajuns la zi, luna sau an
                            while(var2 != NULL)
                            {
                                switch(p)
                                {
                                    case 1:
                                        //ziua
                                        i = atoi(var2);
                                        if(i == 0)
                                            return 0;
                                        else if(i > 31 || i < 1)
                                            return 0;
                                        break;
                                    case 2:
                                        //luna, care e formata din primele 3 caractere ale lunii si trebuie sa respecte formatul acesta
                                        if( strlen(var2) == 3)
                                        {
                                            for( m = 0; m < strlen(var2); m++ )
                                                if(!isalpha(var2[m]))
                                                    return 0;
                                        }
                                        else return 0;
                                        break;
                                    case 3:
                                        //anul.
                                        i = atoi(var2);
                                        if(i == 0)
                                            return 0;
                                        break;
                                }

                                var2 = strtok(NULL, " ");
                                p++;
                            }
                            break;
                        case 4:
                            //cine a adaugat citatul, vom folosi functia searchAuthor dupa ce va fi gata.
                            for( m = 0; m < strlen(var); m++ )
                                if(!isalpha(var[m]))
                                   return 0;
                            break;
                        case 5:
                            //citatul
                            if(strlen(var) == 0)
                                return 0;
                            printf("%d x \n", strlen(var));
                    }
                    var = strtok(NULL, ";"); //trecem la urmatorul sir de car separat de ;
                    k++;
                }
            }
            j++; //trecem la urmatoarea linie
        }
    }
    return 1;
}

And k gets only to 3, so it gets only the number, the author and the date. No quote and no author. So I can’t check them and see if it’s true

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T17:55:44+00:00Added an answer on May 27, 2026 at 5:55 pm

    You can start by taking your first loop away and other variables too.
    The first strtok has to be outside the loop that is going to help you divide each token, this has to be done in order to store the buffer you want to treat in the strtok function.
    You can’t reuse the strtok function until you are certain that you do not want to divide your main data anymore, because if you reuse strtok before the end of the main treatment you are reseting the data used by the strtok function.
    example:

    char str[] = "hello world how are you?\n";
    char *res;
    // here i tell strtok the string str is the one i want to separate
    res = strtok(str, " \n");
    int i = 0;
    // here i separate str, using the caracters space and endline as separators
    while (res != null)
    {
     res = strtok(NULL, " \n"); // each time i pass in this part of the loop i get my new     word in res
     ++i; // here the variable i represents the number of times i enter the loop
    }
    
    // here i can use again strtok with another string
    

    If the sscanf function is allowed in your assignment and since you seem to know the exact format of your file, you may want to use it.
    Also the getline function allows you to fetch line by line of your file, and you could treat each sentence at a time.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an example function below that reads in a date as a string
I have this function on my controller (Im using CodeIgniter) that reads the database,
So I have function that formats a date to coerce to given enum DateType{CURRENT,
In my Java code I have function that gets file from the client in
I have a function that reads a bunch of path-values (8 or 9 of
I have a daemon that reads a configuration file in order to know where
I have created a function that reads lists of ID pairs (i.e. [(A,B),(B,C),(C,D),...] and
I have a function that calls out a read or write request on a
I have some jQuery code that intercepts links clicked on a page: $(document).ready(function() {
I have a simple JavaScript file that has three jQuery $document.ready functions. All three

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.