Writing different node to different files

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

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

#16 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 29 April 2012 - 02:40 AM

Decide your file format first and then make appropriate changes to the read and write functions.
Was This Post Helpful? 0
  • +
  • -

#17 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 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
Was This Post Helpful? 0
  • +
  • -

#18 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 29 April 2012 - 07:59 AM

It's one good solution.
Was This Post Helpful? 0
  • +
  • -

#19 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 30 April 2012 - 01:39 AM

Then how I can read my file to input it into my array of struct?
Was This Post Helpful? 0
  • +
  • -

#20 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 30 April 2012 - 02:22 AM

Reading the same format.
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

Was This Post Helpful? 0
  • +
  • -

#21 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 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.
Was This Post Helpful? 0
  • +
  • -

#22 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 01 May 2012 - 01:33 AM

There are little differences with gets() and fgets(stdin):

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..
Was This Post Helpful? 0
  • +
  • -

#23 DimitriV  Icon User is offline

  • They don't think it be like it is, but it do
  • member icon

Reputation: 583
  • View blog
  • Posts: 2,738
  • Joined: 24-July 11

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.
Was This Post Helpful? 0
  • +
  • -

#24 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 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?
Was This Post Helpful? 0
  • +
  • -

#25 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 01 May 2012 - 05:13 AM

View Postturboscrew, 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.
Was This Post Helpful? 0
  • +
  • -

#26 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 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.
Was This Post Helpful? 0
  • +
  • -

#27 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 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
Was This Post Helpful? 0
  • +
  • -

#28 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 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.

This post has been edited by turboscrew: 01 May 2012 - 01:27 PM

Was This Post Helpful? 0
  • +
  • -

#29 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 - 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?
Was This Post Helpful? 0
  • +
  • -

#30 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 - 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:

Quote

String of characters. This will read subsequent characters until a whitespace is found (whitespace characters are considered to be blank, newline and tab).

Was This Post Helpful? 0
  • +
  • -

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