52 Replies - 2416 Views - Last Post: 27 May 2012 - 11:33 PM
#31
Re: Writing different node to different files
Posted 02 May 2012 - 02:41 AM
#32
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
#33
Re: Writing different node to different files
Posted 02 May 2012 - 06:06 AM
turboscrew, 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
#34
Re: Writing different node to different files
Posted 02 May 2012 - 06:21 AM
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.
#35
Re: Writing different node to different files
Posted 02 May 2012 - 06:43 AM
Quote
jimblumberg, on 02 May 2012 - 08:06 AM, said:
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
#36
Re: Writing different node to different files
Posted 02 May 2012 - 07:21 AM
Quote
gets() doesn't.
Quote
Oh, right. 2 char array is needed. The first char is the wanted one.
Quote
Then the newline just needs to be found and overwritten with '\0'.
#37
Re: Writing different node to different files
Posted 02 May 2012 - 07:30 AM
turboscrew, on 02 May 2012 - 09:21 AM, said:
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
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
#38
Re: Writing different node to different files
Posted 02 May 2012 - 11:02 AM
#39
Re: Writing different node to different files
Posted 02 May 2012 - 11:22 AM
#40
Re: Writing different node to different files
Posted 02 May 2012 - 11:24 AM
while(getchar()!='\n') continue;
#41
Re: Writing different node to different files
Posted 02 May 2012 - 07:53 PM
turboscrew, on 02 May 2012 - 11:22 AM, said:
Ok, I will try it.
aaa111, on 02 May 2012 - 11:24 AM, said:
while(getchar()!='\n') continue;
Using getchar()? Doesn't getchar just saving a character? How it can be used to save string?
#42
Re: Writing different node to different files
Posted 02 May 2012 - 11:29 PM
#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
#43
Re: Writing different node to different files
Posted 07 May 2012 - 12:22 AM
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?
#44
Re: Writing different node to different files
Posted 07 May 2012 - 06:12 AM
Quote
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
#45
Re: Writing different node to different files
Posted 07 May 2012 - 06:39 AM
Quote
strtok_r()?
|
|

New Topic/Question
Reply




MultiQuote



|