Writing different node to different files

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

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

#1 blueboyz  Icon User is offline

  • New D.I.C Head

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

Writing different node to different files

Posted 25 April 2012 - 03:22 AM

This is my node and my array of struct

typedef struct mhs mhs;
typedef struct kelas kelas;

struct mhs
{
    char nama[31];
    char nim[16];
    int angkatan;
    float ipk;
};

struct kelas
{
    char kls[13];
    int jml;
    mhs siswa[40];
    kelas *next;
};

kelas=node and mhs= array of struct.

I want to write those kelas to different .txt / .csv with name file from kelas->kls.
So if I have 3 kelas that means I write to 3 file.csv

How to do that? I never used function write to file before. Can you tell me how to do it? Trying to googling it but can't find good answers.
Then I try to do it like this
int printNodesToFile() {
    char filename[13];
    kelas *itr=head;
    int idx;
    for(idx = 1; itr = itr->next, idx++; itr!= NULL) {
        snprintf(filename, sizeof(filename), "kelas%d.csv", idx);
        FILE* outFile = fopen(filename, "w");
        if(outFile == NULL) return 1;
        else
        {
            for(int i=0;i<itr->jml;i++)
                fprintf(outFile,"%d. %s; %s; %d; %.2f\n",i+1,itr->siswa[i].nama,itr->siswa[i].nim,itr->siswa[i].angkatan,itr->siswa[i].ipk);
            fclose(outFile);
        }
    }
    return 0;
}


but it print character which can't be unread.

Is This A Good Question/Topic? 0
  • +

Replies To: Writing different node to different files

#2 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 25 April 2012 - 03:59 AM

Because everything here looks good, I wonder how the nama and nim are read? Are you sure they contain the NULL-character after the read characters? If not, then B***S**t from uninitialised part of the character vectors are written out as text until the first NULL-character is encountered.

Maybe as a debug you could write out the numeric values of the characters contained in nama and nim?
if there are values less than 32, you'll see smiling faces and spade-symbols and the like when you print them in text.

Another way could be strlen() to see that the string lengths don't exceed the storage space.
Was This Post Helpful? 0
  • +
  • -

#3 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 25 April 2012 - 07:34 AM

Okay. Now I can get it works. But I still can't use my kelas->kls as my filename. Can you help me? I am already trying to do like this
snprintf(filename, sizeof(filename), "%s.csv", itr->kls);

But it didn't make any file.

And I also got problem about printing my data to file. When I try to print any string it will also contains \n which annoy me. Is there any predefined function to clearing it? Or I need to make my own function. If yes, how to do that?
Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg  Icon User is offline

  • member icon


Reputation: 4071
  • View blog
  • Posts: 12,557
  • Joined: 25-December 09

Re: Writing different node to different files

Posted 25 April 2012 - 08:12 AM

Have you tried printing your filename string when the file doesn't open?

Jim
Was This Post Helpful? 0
  • +
  • -

#5 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 25 April 2012 - 09:20 AM

Yes. It shows nothing

Yes. It shows nothing
Was This Post Helpful? 0
  • +
  • -

#6 jimblumberg  Icon User is offline

  • member icon


Reputation: 4071
  • View blog
  • Posts: 12,557
  • Joined: 25-December 09

Re: Writing different node to different files

Posted 25 April 2012 - 09:41 AM

So what does itr->kls contain? Where do you assign a value to head? This is part of the reason you should avoid using global variables and pass the required parameters into your function. I would say that the problem is before this function.

Jim
Was This Post Helpful? 0
  • +
  • -

#7 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 25 April 2012 - 06:25 PM

Ok. Its fixed now.
Btw what about parsing my file to be written into my program again then write it to my struct.
Can you tell me the concept code to do it?
Looks like I will use new input to determine what file which I need to parse to my struct. I use "," as my limiter.
Thanks.
Was This Post Helpful? 0
  • +
  • -

#8 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 26 April 2012 - 01:53 AM

You mean something like
fscanf(infile,"%d. %s, %s, %d, %.2f\n",&i,&itr->siswa[i].nama,&itr->siswa[i].nim,&itr->siswa[i].angkatan,&itr->siswa[i].ipk);
?
Was This Post Helpful? 0
  • +
  • -

#9 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 26 April 2012 - 02:53 PM

I am trying to do like this
void writeFileToNode(char *klas)
void writeFileToNode(char *klas)
{
    FILE *infile=fopen(klas,"r");
    if (infile==NULL)
    {
        printf("Gagal import dari file %s",klas);
        getch();
    }
    else
    {
        klas[(strlen(klas)-4)] = '\0';
        insertKelas(klas);
        for(int i=0;i<41;i++)
                fscanf(infile,"%s\n%s\n%d\n%f\n",head->siswa[i].nama,head->siswa[i].nim,&head->siswa[i].angkatan,&head->siswa[i].ipk);
        fclose(infile);
        printf("Anda telah berhasil mengimport\n");
        getch();
    }
}

but it can not inputting to my array of struct. This is my insertKelas function.
void insertKelas(char *klas)
{
   kelas *newNode=malloc(sizeof(kelas));
   if(newNode != NULL)
   {
      strcpy(newNode->kls,klas);
      newNode->jml=0;
      newNode->next=head;
      head=newNode;
      nodeKelas++;
   }
   else printf("Gagal menambah kelas");
   getch();
}

I am using global variable for my head. Please don't tell me to change it become local variable. I can not do that for now. I am really lazy to change all my code and I need to do it with fast.

Anyway this is my file.csv look like

albilaga linggra
15060
2011
3.45
arik achmad
050
2010
3.15
grandis mahendra
070
2010
3.56



Actually I want to change the limiter to ',' but because I am getting error with my input function then I change it to '\n'
Was This Post Helpful? 0
  • +
  • -

#10 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 26 April 2012 - 10:43 PM

You are reading: "%s\n%s\n%d\n%f\n".
I understood you wrote "%d. %s; %s; %d; %.2f\n"

You are reading different format from what you write.
You read the stuff in different order and one value per line.
Was This Post Helpful? 0
  • +
  • -

#11 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 26 April 2012 - 11:43 PM

I already change my function print to file to be like this

fprintf(outFile,"%s\n%s\n%d\n%.2f\n\n",itr->siswa[i].nama,itr->siswa[i].nim,itr->siswa[i].angkatan,itr->siswa[i].ipk);

Was This Post Helpful? 0
  • +
  • -

#12 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 27 April 2012 - 09:12 AM

You were reading: "%s\n%s\n%d\n%f\n"

=>
string, new line, string, new line, integer, new line, flost, new line.

in your file you hat 2 strings on the same line.

This post has been edited by turboscrew: 27 April 2012 - 09:13 AM

Was This Post Helpful? 0
  • +
  • -

#13 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 April 2012 - 09:13 PM

What do you mean? I didn't get it
Was This Post Helpful? 0
  • +
  • -

#14 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 28 April 2012 - 01:14 AM

I meant that the file format doesn't seem to keep consistent.

This:

Quote

"%s\n%s\n%d\n%f\n"

means that:
(%s\n) Read characters into a string until newline - discard the
       newline.

(%s\n) Read characters into a string until newline - discard the
       newline.

(%d\n) Read characters until a digit is found - discard non-digits read.
       Read digit characters until a non-digit is found and try to make
       the read digits into an integer. Then read until newline and drop
       the read characters.

(%f\n) read characters until a digit or "." is found - discard
       non-digits read.
       read digit characters until a non-digit is found and try to make
       the read digits into an float. Then read until newline and drop
       the read characters.



and you say your file looks like:
albilaga linggra
15060
2011
3.45
arik achmad
050
2010
3.15



In your code:

Quote

fscanf(infile,"%s\n%s\n%d\n%f\n"

you read:
"albilaga linggra" as the first string
"15060" as the second string
"2011" as the integer
"3.15" as the float

but you write that:

Quote

fprintf(outFile,"%s\n%s\n%d\n%.2f\n\n"

with an empty line between the entries. Next time the empty lines are read in as empty strings.
Was This Post Helpful? 0
  • +
  • -

#15 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 28 April 2012 - 09:42 PM

So what should I do? Changing my fprintf function or my fscanf function?
Was This Post Helpful? 0
  • +
  • -

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