Writing different node to different files

  • (4 Pages)
  • +
  • « First
  • 2
  • 3
  • 4

52 Replies - 4574 Views - Last Post: 27 May 2012 - 11:33 PM Rate Topic: -----

#46 jimblumberg  Icon User is offline

  • member icon


Reputation: 4018
  • View blog
  • Posts: 12,401
  • Joined: 25-December 09

Re: Writing different node to different files

Posted 07 May 2012 - 06:44 AM

View Postturboscrew, on 07 May 2012 - 08:39 AM, said:

strtok_r()?

Why? There is no need of the strtok_r() in a single threaded program. And the OP will have enough problems with strtok() without any added complexity.

Jim
Was This Post Helpful? 0
  • +
  • -

#47 turboscrew  Icon User is offline

  • D.I.C Addict

Reputation: 100
  • View blog
  • Posts: 615
  • Joined: 03-April 12

Re: Writing different node to different files

Posted 07 May 2012 - 07:27 AM

Just to learn to use strtok_r() instead of strtok().

Oh, and strtok_r() calls can be nested while strtok() calls cannot.

This post has been edited by turboscrew: 07 May 2012 - 07:31 AM

Was This Post Helpful? 0
  • +
  • -

#48 jimblumberg  Icon User is offline

  • member icon


Reputation: 4018
  • View blog
  • Posts: 12,401
  • Joined: 25-December 09

Re: Writing different node to different files

Posted 07 May 2012 - 07:34 AM

If you want to learn to use that function then fine you go ahead, but this OP is having enough problems with basic file handling, I don't recommend trying to use advanced concepts unless absolutly necessary.

Quote

Oh, and strtok_r() calls can be nested while strtok() calls cannot.

Two things, first strtok_r() is not a standard function, and may not be available with all compilers. Second the only major difference between the two is that you must provide the buffer for use with strtok_r().

See this link, and from that link:

Quote

The strtok_r() function is a reentrant version strtok(). The saveptr argument is a pointer to a char * variable that is used internally by strtok_r() in order to maintain context between successive calls that parse the same string.


Jim

This post has been edited by jimblumberg: 07 May 2012 - 07:42 AM

Was This Post Helpful? 0
  • +
  • -

#49 turboscrew  Icon User is offline

  • D.I.C Addict

Reputation: 100
  • View blog
  • Posts: 615
  • Joined: 03-April 12

Re: Writing different node to different files

Posted 08 May 2012 - 03:48 AM

I have learned: 15+ years of C programming for living...
Started C programming mid 80's when ANSI was still not interested in C and the "standard" was K&R.

You seem to have pretty strong OPINIONS.
Was This Post Helpful? 0
  • +
  • -

#50 blueboyz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 24-April 12

Re: Writing different node to different files

Posted 23 May 2012 - 11:32 AM

Hello... Now I touching this project again. How I can read all files to just one variables in fgets and how I limit my string length in fgets? It will read whole character in file right?

And anyway, I remember someone told me to not using fflush(stdin) because it will makes undefined behaviour. But how I can remove buffer from my input if not using this? Because in my program if I try to stop my buffer with getchar() it works for sometimes like if I just input my data once. If I input it twice getchar() won't work and it will vanishing my 1st char.

I hope somebody still remember this thread :D
Was This Post Helpful? 0
  • +
  • -

#51 jimblumberg  Icon User is offline

  • member icon


Reputation: 4018
  • View blog
  • Posts: 12,401
  • Joined: 25-December 09

Re: Writing different node to different files

Posted 24 May 2012 - 12:54 PM

I don't understand your question. You may want to restate clearly your problem. Maybe show a small program that illustrates your question/problem.

Quote

How I can read all files to just one variables in fgets and how I limit my string length in fgets?

The fgets ( char * str, int num, FILE * stream ) function reads at most num characters from the file stream and places them into the C-string str. All characters will be processed until either num characters have been retrieved or the end of line character is encountered. The end of line character is included in your str if it was encountered. You will need to remove the end of line character manually. Usually like:

// Replace the last character of your string with the end of string character.
  str[strlen(str)-1] = '\0'; 


Quote

It will read whole character in file right?

If your file has more than one line you will need some kind of loop to process all the lines contained in the file.

Quote

But how I can remove buffer from my input if not using this? Because in my program if I try to stop my buffer with getchar() it works for sometimes like if I just input my data once. If I input it twice getchar() won't work and it will vanishing my 1st char.

If you are trying to insure there are no characters left in your input buffer then you can remove and discard any remaining characters with something like:
   int c;
   while ((c = getchar()) != '\n') ; 


The above code will retrieve and discard all characters until the end of line character is encountered.

When switching between input modes, scanf() to fgets() you will usually need to remove the end of line character because scanf() leaves this character in the input buffer which will mess up the read of a character. It is sometimes easier to always retrieve input with fgets() then use sscanf() to process this string extracting your variables out of the retrieved string.


Jim
Was This Post Helpful? 0
  • +
  • -

#52 turboscrew  Icon User is offline

  • D.I.C Addict

Reputation: 100
  • View blog
  • Posts: 615
  • Joined: 03-April 12

Re: Writing different node to different files

Posted 24 May 2012 - 09:39 PM

I think jimblumberg's last suggestion might be best for you at this point: Reading the line in a string using fgets() and then reading the values to the variables from the string using sscanf().
Was This Post Helpful? 0
  • +
  • -

#53 blueboyz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 24-April 12

Re: Writing different node to different files

Posted 27 May 2012 - 11:33 PM

I am already trying to do it like this
void writeFileToNode(char *klas,int lng)
{
    char *file_any;
    FILE *infile=fopen(klas,"r");
    if (infile==NULL)
    {
        printf("File %s tidak ditemukan",klas);
        getch();
    }
    else
    {
        fgets(file_any,99,infile);
        klas[(strlen(klas)-4)] = '\0';
        insertKelas(klas,lng);
        while(strtok(file_any,",")!=NULL)
        {
            printf("%s",file_any);
        }



But why it can't input to my program?
Was This Post Helpful? 0
  • +
  • -

  • (4 Pages)
  • +
  • « First
  • 2
  • 3
  • 4