Decide your file format first and then make appropriate changes to the read and write functions.
52 Replies - 2436 Views - Last Post: 27 May 2012 - 11:33 PM
#17
Re: Writing different node to different files
Posted 29 April 2012 - 07:38 AM
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
but because I have problem in my inputing. Let me explain my problem 1st:
If I use scanf, I can't read whitespace.
If I use fgets, I need to use getchar() or fflush() to can read my input keyboard. But it will also read \n
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
mhs[2].name
mhs[2].nim
mhs[2].angkatan
mhs[2].ipk
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
but because I have problem in my inputing. Let me explain my problem 1st:
If I use scanf, I can't read whitespace.
If I use fgets, I need to use getchar() or fflush() to can read my input keyboard. But it will also read \n
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
mhs[2].name
mhs[2].nim
mhs[2].angkatan
mhs[2].ipk
#18
Re: Writing different node to different files
Posted 29 April 2012 - 07:59 AM
It's one good solution.
#19
Re: Writing different node to different files
Posted 30 April 2012 - 01:39 AM
Then how I can read my file to input it into my array of struct?
#20
Re: Writing different node to different files
Posted 30 April 2012 - 02:22 AM
Reading the same format.
Which fields can have whitespaces?
Why? And what's wrong with gets()?
You can mix scanf and (f)gets.
You can also read temporary strings with scanf and append them with whitespaces into the field.
Which fields can have whitespaces?
Quote
If I use fgets, I need to use getchar() or fflush() to can read my input keyboard.
Why? And what's wrong with gets()?
You can mix scanf and (f)gets.
You can also read temporary strings with scanf and append them with whitespaces into the field.
This post has been edited by turboscrew: 30 April 2012 - 02:23 AM
#21
Re: Writing different node to different files
Posted 01 May 2012 - 01:07 AM
gets and fgets is same. If I don't add getchar() or fflush() first, it will read my string with just "\n". So I can't input anything because they directly read my input as "\n". If I use scanf("%s") it will can't read white space. If I use scanf("[^\n]"), it will read like gets and fgets.
#22
Re: Writing different node to different files
Posted 01 May 2012 - 01:33 AM
There are little differences with gets() and fgets(stdin):
See here..
Quote
Notice that gets does not behave exactly as fgets does with stdin as argument: First, the ending newline character is not included with gets while with fgets it is. And second, gets does not let you specify a limit on how many characters are to be read, so you must be careful with the size of the array pointed by str to avoid buffer overflows.
See here..
#23
Re: Writing different node to different files
Posted 01 May 2012 - 01:37 AM
Yes, it will read \n as a new line in strings: it is an escape character.
#24
Re: Writing different node to different files
Posted 01 May 2012 - 01:57 AM
On the other hand, you probably don't want the newline into your stings, but you probably want the newline read away fron the input buffer, so what's so bad in reading the newline off separately?
#25
Re: Writing different node to different files
Posted 01 May 2012 - 05:13 AM
turboscrew, on 01 May 2012 - 01:57 AM, said:
On the other hand, you probably don't want the newline into your stings, but you probably want the newline read away fron the input buffer, so what's so bad in reading the newline off separately?
Can you explain it more clearly what do you mean? Can I get rid of \n from my input and can read white space too? Actually I just need this.
#26
Re: Writing different node to different files
Posted 01 May 2012 - 07:23 AM
I mean, what's wrong if you read the stuff with gets and then read off the newline with getchar? The filepointer advances anyway, so getchar doesn't read what gets has already read.
#27
Re: Writing different node to different files
Posted 01 May 2012 - 12:04 PM
If I read my input with newline that's mean I can't make my file looks like.csv file
#28
Re: Writing different node to different files
Posted 01 May 2012 - 01:25 PM
Do you really want your string variables to contain newlines?
If you print such variable like:
fprintf(filep, "%s\n", angkatan);
you get 2 newlines - the one from the variable and the other from the format.
If you print such variable like:
fprintf(filep, "%s\n", angkatan);
you get 2 newlines - the one from the variable and the other from the format.
This post has been edited by turboscrew: 01 May 2012 - 01:27 PM
#29
Re: Writing different node to different files
Posted 02 May 2012 - 12:41 AM
Nope. For string I just declared it like
fprintf(filep, "%s", angkatan);
But I don't know why I can't use
fscanf(filep, "%s", angkatan);
neither
fscanf(filep, "%s\n", angkatan);
to read my file and put it into my program?
fprintf(filep, "%s", angkatan);
But I don't know why I can't use
fscanf(filep, "%s", angkatan);
neither
fscanf(filep, "%s\n", angkatan);
to read my file and put it into my program?
#30
Re: Writing different node to different files
Posted 02 May 2012 - 12:52 AM
You can, if the input string doesn't contain blanks.
It'd be even recommended.
You can also read "multi-word" strings word-by-word if you know how many words there will be, and concatenate the "sunstrings" with blanks together to for a single string containing blanks.
The manual says about fscanf and %s:
It'd be even recommended.
You can also read "multi-word" strings word-by-word if you know how many words there will be, and concatenate the "sunstrings" with blanks together to for a single string containing blanks.
The manual says about fscanf and %s:
Quote
String of characters. This will read subsequent characters until a whitespace is found (whitespace characters are considered to be blank, newline and tab).
|
|

New Topic/Question
Reply




MultiQuote


|