Writing different node to different files

  • (4 Pages)
  • +
  • 1
  • 2
  • 3
  • 4

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

#31 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 02 May 2012 - 02:41 AM

But I can't determine how many user will input character, word and how many whitespace is used. How I can do that?
Was This Post Helpful? 0
  • +
  • -

#32 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 02 May 2012 - 03:43 AM

albilaga linggra    // Use gets() and getchar()
15060               // Use scanf
2011                // Use scanf
3.45                // Use scanf


Was This Post Helpful? -1
  • +
  • -

#33 jimblumberg  Icon User is online

  • member icon


Reputation: 4013
  • View blog
  • Posts: 12,393
  • Joined: 25-December 09

Re: Writing different node to different files

Posted 02 May 2012 - 06:06 AM

View Postturboscrew, on 02 May 2012 - 05:43 AM, said:

albilaga linggra    // Use gets() and getchar()


Never, Never, Never use gets(). The gets() function, and any input function that does not limit the length of the string are buffer overruns waiting to happen. Always use a function that limits the length of your input to the size of your buffer. This function is considered so bad that it has been removed from the current C standard. Also note this includes the scanf() function if you do not use the size modifier.

The problem of mixing scanf() and fgets() is that in most cases scanf() leaves the end of line character in the input buffer. The next retrieval operation, if character, will retrieve this end of line character for it's complete entry. This problem also affects scanf() when retrieving a character from the input buffer with the "%c" specifier. Also remember that fgets() consumes this end of line character and stores it in the string, so you will need to remove this character from your string.

Jim
Was This Post Helpful? 0
  • +
  • -

#34 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 02 May 2012 - 06:21 AM

The idea was that gets reads the input 'till the newline and getchar reads away the newline.

To me it was told a decade or two ago not to use scanf with %c.
Use "%1s" rather.

On the other hand, I'm brave enough to use even strtok.
Was This Post Helpful? 0
  • +
  • -

#35 jimblumberg  Icon User is online

  • member icon


Reputation: 4013
  • View blog
  • Posts: 12,393
  • Joined: 25-December 09

Re: Writing different node to different files

Posted 02 May 2012 - 06:43 AM

Quote

The idea was that gets reads the input 'till the newline and getchar reads away the newline.

View Postjimblumberg, on 02 May 2012 - 08:06 AM, said:

Never, Never, Never use gets().


The gets() function does not limit the number of characters it will attempt to retrieve, and is considered "EVIL". Use something that limits the number of characters it will retrieve like fgets().

Also both gets() and fgets() retrieve the end of line character. The problem is when this end of line character is left in the input buffer by some other function, like scanf() prior to the fgets() call.

Also you can not use the "%1s" scanf() modifier to retrieve a single character into a char variable. This modifier may only retrieve one character from the buffer but it will try to store two characters into your single char, the character retrieved and the end of string character.

Jim

This post has been edited by jimblumberg: 02 May 2012 - 06:47 AM

Was This Post Helpful? 1
  • +
  • -

#36 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 02 May 2012 - 07:21 AM

Quote

Also both gets() and fgets() retrieve the end of line character.


gets() doesn't.

Quote

Also you can not use the "%1s" scanf() modifier to retrieve a single character into a char variable.


Oh, right. 2 char array is needed. The first char is the wanted one.

Quote

Use something that limits the number of characters it will retrieve like fgets().

Then the newline just needs to be found and overwritten with '\0'.
Was This Post Helpful? -1
  • +
  • -

#37 jimblumberg  Icon User is online

  • member icon


Reputation: 4013
  • View blog
  • Posts: 12,393
  • Joined: 25-December 09

Re: Writing different node to different files

Posted 02 May 2012 - 07:30 AM

View Postturboscrew, on 02 May 2012 - 09:21 AM, said:

Quote

Also both gets() and fgets() retrieve the end of line character.


gets() doesn't.

The evil gets() function does indeed extract this end of line character, but it does not place this character in the string, it discards it.

See this link: gets(), and from that link:

Quote

The gets() function shall read bytes from the standard input stream, stdin, into the array pointed to by s, until a <newline> is read or an end-of-file condition is encountered. Any <newline> shall be discarded and a null byte shall be placed immediately after the last byte read into the array.

But as I have said multiple times: Never Never Never use gets!!!!!!!!!!!

Jim

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

Was This Post Helpful? 0
  • +
  • -

#38 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 02 May 2012 - 11:02 AM

So what should I do to discard \n from my input? Am I need to make own function? Isn't that slowing my program?
Was This Post Helpful? 0
  • +
  • -

#39 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 02 May 2012 - 11:22 AM

How about fgets (like jimplumberg suggested) and then finding the newline in the string and replacing it with NUL-character ('\0').
Was This Post Helpful? 0
  • +
  • -

#40 aaa111  Icon User is offline

  • D.I.C Regular

Reputation: 88
  • View blog
  • Posts: 284
  • Joined: 21-February 07

Re: Writing different node to different files

Posted 02 May 2012 - 11:24 AM

One solution is the following:
while(getchar()!='\n')
	continue;


Was This Post Helpful? 0
  • +
  • -

#41 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 02 May 2012 - 07:53 PM

View Postturboscrew, on 02 May 2012 - 11:22 AM, said:

How about fgets (like jimplumberg suggested) and then finding the newline in the string and replacing it with NUL-character ('\0').

Ok, I will try it.

View Postaaa111, on 02 May 2012 - 11:24 AM, said:

One solution is the following:
while(getchar()!='\n')
	continue;


Using getchar()? Doesn't getchar just saving a character? How it can be used to save string?
Was This Post Helpful? 0
  • +
  • -

#42 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 02 May 2012 - 11:29 PM

Like
  #define MAX_LEN 30
  int i=0;
  char ch;
  char str1[MAX_LEN];
  while (ch = getchar() != '\n') 
  { 
    str1[i] = ch;
    i++;
    /* buffer-check and room for end-NUL to keep jimplumberg happy ;-) */
    if (i >= MAX_LEN - 1) break;
  }
  str1[i] = '\0';



Better still would be a loop that reads the characters until newline (the newline included), but only stores the read characters when the buffer-check allows.

This post has been edited by turboscrew: 02 May 2012 - 11:32 PM

Was This Post Helpful? 1
  • +
  • -

#43 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 07 May 2012 - 12:22 AM

Ok, now I can fixed my input. Thanks a lot.
Anyway I still can't read from file to my program. This is my file look alike.
albi,1150,2011,3.28
arik,1050,2010,2.50



And this is my code :
void writeFileToNode(char *klas)
{
    char nama[31],nim[16];
    int angkatan;
    float ipk;
    FILE *infile=fopen(klas,"r");
    if (infile==NULL)
    {
        printf("File %s tidak ditemukan",klas);
        getch();
    }
    else
    {
        klas[(strlen(klas)-4)] = '\0';
        insertKelas(klas);
        kelas *itr=head;
        //this is my looping
        for(int i=0;i<40;i++)
            //this is how I input it to my program
            fscanf(infile,"%30s,%15s,%d,%f\n",itr->siswa[i].nama,itr->siswa[i].nim,&itr->siswa[i].angkatan,&itr->siswa[i].ipk);
        fclose(infile);
        printf("Anda telah berhasil mengimport\n");
        getch();
    }
}


Can anybody help me? :D
Was This Post Helpful? 0
  • +
  • -

#44 jimblumberg  Icon User is online

  • member icon


Reputation: 4013
  • View blog
  • Posts: 12,393
  • Joined: 25-December 09

Re: Writing different node to different files

Posted 07 May 2012 - 06:12 AM

Quote

I actually want my format to be like this:
mhs[0].name,mhs[0].nim,mhs[0].angkatan,mhs[0].ipk
mhs[1].name,mhs[1].nim,mhs[1].angkatan,mhs[1].ipk
mhs[2].name,mhs[2].nim,mhs[2].angkatan,mhs[2].ipk

Thats why I decided to change it like this:

mhs[0].name
mhs[0].nim
mhs[0].angkatan
mhs[0].ipk

mhs[1].name
mhs[1].nim
mhs[1].angkatan
mhs[1].ipk

You need to make up your mind how the data file will be formatted. Until you have a consistent data file you will not be able to reliably read the file. If you want to read Comma Delimited Values then you should be using fgets() to retrieve a complete line from you file into a buffer then use strtok() to parse the buffer.

Jim
Was This Post Helpful? 0
  • +
  • -

#45 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 - 06:39 AM

Quote

then use strtok() to parse the buffer.

strtok_r()?
Was This Post Helpful? 0
  • +
  • -

  • (4 Pages)
  • +
  • 1
  • 2
  • 3
  • 4