Like Tree5Likes

Linked Lists please help!!!

This is a discussion on Linked Lists please help!!! within the C Programming forums, part of the General Programming Boards category; fwrite() writes bytes to the file. So if you have for example an int value 10 and your system uses ...

  1. #46
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    fwrite() writes bytes to the file.

    So if you have for example an int value 10 and your system uses 4 bytes for an int, then on a little endian system the value is stored as 0a 00 00 00 and on a big endian system it is stored as 00 00 00 0a. fwrite() will write these 4 bytes to the file.

    If you open the file in a text editor, the editor interprets these bytes as characters (in ASCII one byte/character). That's why you get strange symbols.

    You should write the numbers to your text file with fprintf().

    Bye, Andreas

  2. #47
    Registered User
    Join Date
    Nov 2012
    Posts
    49
    I change the line with fprintf() but nothing... I actually did this :

    Code:
       .......
       .......
       srand( time(NULL) );
    
        for ( i = 0 ; i < N ; i++ )
        {
            random_number = rand()%LIMIT;
            fprintf( fp, "%s\n", &random_number); //This is the line i have changed
            printf ( "%d\t", random_number) ;
        }
        fclose(fp);
    Please, i appreciate if you tell me how i should change that line to make my program work fine.

    Thank you.

  3. #48
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,303
    You seem to have got it right in the next line:
    Code:
    printf ( "%d\t", random_number) ;
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #49
    Registered User
    Join Date
    Nov 2012
    Posts
    49
    I don't understand i should use fprintf() or not...??? And if is ok the way i have changed it why it still doesn't print the numbers in the input.txt???

  5. #50
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,303
    Quote Originally Posted by ValL
    I don't understand i should use fprintf() or not...?
    If you want to print to a file, sure. The problem is that you are using a wrong format specifier. It should have been:
    Code:
    fprintf(fp, "%d\n", random_number);
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #51
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,303
    Quote Originally Posted by ValL
    1. return type of 'main' is not 'int'

    In function 'main':
    What this means is that you should change void main to int main.

    Quote Originally Posted by ValL
    2. passing argument 1 of 'fprintf' from incompatible pointer type
    3. expected 'struct FILE *' but argument is of type 'char *'
    4. passing argument 2 of 'fprintf' makes pointer from integer without a cast
    5. expected 'const char *' but argument is of type 'int'
    These should not be happening if you changed the fprintf call to what I described.

    I suggest that you post your current code and the corresponding error messages.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #52
    Registered User
    Join Date
    Nov 2012
    Posts
    49
    Ok thank you for your help. I have fixed it. It seems that the problem was in main something.

  8. #53
    Registered User
    Join Date
    Nov 2012
    Posts
    49
    Hello again. I have an issue in a program i made. Actually i made this: I created a file and i stored into it, an array of random numbers. First i have to tell the user to give how many numbers wants to put into the array and let's say he says 5 the first number of the array will be 5. So i did it but i have managed to do all code in main, so i want know to create functions and call them in main when i need them. My problem is that i have tried to create the functions but i can not make them work properly. Can anyone help me and tell me how i am supposed to do the functions?

    This is the hole code without the functions i have done so far:

    Code:
    # include <stdio.h>
    # include <conio.h>
    # include <stdlib.h>
    # include <time.h>
    
    # define LIMIT 1000
    
    int main()
    {
        FILE *fp ;
        char str[67] ;//This is an Array of ch where we store the name of the file
        int i, N, random_number;
        int Array[N]; //Create an Array[] of size N
    
        //clrscr() ;
    
        printf ( "Please give the name of the file: " ) ;
        scanf ( "%s", str ) ;
    
        printf ( "Please give the element of numbers in the array you want: " ) ;
        scanf ( "%d", &N ) ;
        printf("\n");
    
        fp = fopen ( str, "wb" ) ;
        if ( fp == NULL )
        {
            printf ( "The file can not be created." ) ;
            getch() ;
            exit (0) ;
        }
    
        srand ( time(NULL) );
    
        Array[0] = N;
        fprintf(fp, "%d \n", Array[0]);
        printf("%d\t", Array[0]);
    
        for ( i = 1; i < N; i++ )
        {
            Array[i] = rand()%LIMIT;
            fprintf(fp, "%d \n\n ",Array[i]);
            printf( "%d\t", Array[i]) ;
        }
    
        fclose (fp) ;
        printf("\n");
        printf ( "\nThe file has been created." ) ;
    
    return 0;
    system("pause");
    }
    Thank you in advance!!

  9. #54
    Registered User
    Join Date
    May 2009
    Posts
    2,770
    Do you understand why this code is bad?

    Code:
    return 0;
    system("pause");
    The return statement will end the process flow in the function and the system command will never be called.

    Tim S.
    AndiPersti likes this.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  10. #55
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Code:
    int i, N, random_number;
    int Array[N]; //Create an Array[] of size N
    That doesn't work. "N" is used uninitialized so it contains garbage. You can't declare the array before you read a value for "N".

    Code:
    fp = fopen ( str, "wb" ) ;
    You write the numbers as text, thus you shouldn't open the file in binary mode.

    Quote Originally Posted by ValL View Post
    My problem is that i have tried to create the functions but i can not make them work properly. Can anyone help me and tell me how i am supposed to do the functions?
    Have you read already about functions in C?

    What functions do you want to create? What problems do you have (show us your attempts)?

    Bye, Andreas

  11. #56
    Registered User
    Join Date
    Nov 2012
    Posts
    49
    Actually the exercise says to create a program where you create a file and store into it an array of integer random numbers with first number the N (length of the array the user wants) for example if he wants 5 elements in the array the first number of the array will be 5 and the other 4 the random numbers are created.

    Then it says to sort the array with the heap sort and put the sorted array in another file. I sent you that program i have tried because i am trying to do it piece by piece see that it works and then continue with the other pieces of the program. I have read about the files but i can't understand the way i have to copy the array from one file to the other. I will appreciate it a lot if you explain me how i can make this step. And after that i will try the other steps us well!!!

    Thank you
    Last edited by ValL; 11-28-2012 at 01:16 PM.

  12. #57
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by ValL View Post
    Actually the exercise says to create a program where you create a file and store into it an array of integer random numbers with first number the N (length of the array the user wants) for example if he wants 5 elements in the array the first number of the array will be 5 and the other 4 the random numbers are created.

    Then it says to sort the array with the heap sort and put the sorted array in another file. I sent you that program i have tried because i am trying to do it piece by piece see that it works and then continue with the other pieces of the program. I have read about the files but i can't understand the way i have to copy the array from one file to the other. I will appreciate it a lot if you explain me how i can make this step. And after that i will try the other steps us well!!!
    Ok, you have to write an integer array to a file twice.

    So for a start I suggest writing a function (e.g. "write_array") which takes three parameters (a file pointer, an array of ints, the size of the array) and write the contents of the array to the given file. You have already written code for this task. Are you able to extract these code lines and put them into the function?

    Bye, Andreas

  13. #58
    Registered User
    Join Date
    Nov 2012
    Posts
    49
    Yes, ok thank you so much for your help i will try to do it like you said and i will tell you if is working.

  14. #59
    Registered User
    Join Date
    Nov 2012
    Posts
    49
    Ok my friend i did what you told me as first step for the exercise... And it seems to wok fine but let me sent you the part i did to check it maybe i have done something wrong!!

    Code:
    # include <stdio.h>
    # include <conio.h>
    # include <stdlib.h>
    # include <time.h>
    
    # define LIMIT 1000
    
    void create_file(FILE *fp, int *Array,int N)
    {
        int i;
    
        if ( fp == NULL )
        {
            printf ( "The file can not be created." ) ;
            getch() ;
            exit (0) ;
        }
    
        srand ( time(NULL) );
    
        for ( i = 1; i < N; i++ )
        {
            Array[i] = rand()%LIMIT;
            fprintf(fp, "%d ",Array[i]);
            printf( "%d ", Array[i]) ;
        }
    }
    
    void main()
    {
        char file_name[50];
        int N;
        FILE *fp;
    
        printf("Please give name of the file you want to create: ");
        scanf("%s", file_name);
        printf("Please give how many numbers you want to put in the array: ");
        scanf("%d", &N);
    
        int Array[N];
    
        fp = fopen(file_name, "w");
    
        Array[0] = N;
        fprintf(fp, "%d ", Array[0]);
        printf("%d ", Array[0]);
    
        create_file(fp,Array,N);
    
    }
    Can you help me now and with the other steps? Like sorting the array and put it in another file? I am so confused that's why i am asking you help.

    Thank you.
    Last edited by ValL; 11-28-2012 at 02:32 PM.

  15. #60
    Registered User
    Join Date
    Nov 2012
    Posts
    49

Page 4 of 5 FirstFirst 12345 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Double Linked Dynamic Lists Vs Unrolled Linked Lists
    By lantzvillian in forum C Programming
    Replies: 6
    Last Post: 02-14-2012, 01:07 PM
  2. Replies: 4
    Last Post: 05-01-2010, 11:19 PM
  3. Question about Linked lists of lists
    By hear_no_evil in forum C Programming
    Replies: 2
    Last Post: 11-08-2004, 02:49 AM
  4. question on linked lists(stack with linked lists)
    By dionys in forum C Programming
    Replies: 1
    Last Post: 06-02-2004, 12:08 PM
  5. Linked List of Linked lists Revisited.
    By Qui in forum C++ Programming
    Replies: 11
    Last Post: 04-11-2004, 10:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21