Thread: Read

  1. #1
    Unregistered
    Guest

    Read

    Hi i am writing this function to read a txt file where there's two
    line of text

    Name Surname
    ID_NUMBER

    and usig the following file_read () i want to display it on the screen and the following is the code that i am using any idea on why is not working ?

    file_read()
    {
    FILE *fp;

    char Name[30];

    fp = fopen("id.txt","r");

    if ((fp = fopen("id.txt","r")) == NULL)

    {
    printf("Unabale to open id.txt file \n");
    exit(1);
    }

    while(fgets,(Name,30,fp) != NULL){

    }
    fclose(fp);
    }


    Thank you !

  2. #2
    Registered User C_Coder's Avatar
    Join Date
    Oct 2001
    Posts
    522
    fp = fopen("id.txt","r");

    if ((fp = fopen("id.txt","r")) == NULL)

    {
    printf("Unabale to open id.txt file \n");
    exit(1);
    }
    You open the file with the first line and then in your if statement you try to open it again and as the file is already open this will produce a NULL response every time, remove the first line.

    while(fgets,(Name,30,fp) != NULL){
    lose the comma which is after fgets and before the first (
    All spelling mistakes, syntatical errors and stupid comments are intentional.

  3. #3
    Unregistered
    Guest
    Thanx C_Coder it compiles fine and i now have another problem is not displaying the content of the file.




    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>


    main()

    {

    FILE *fp;


    char Name[30];




    fp = fopen("a:/id.txt","r");

    if ((fp = fopen("a:/id.txt","r")) == NULL)

    {
    printf("Unabale to open id.txt file \n");
    exit(1);
    }

    while(fgets(Name,30,fp) != NULL){

    }

    fclose(fp);
    }

  4. #4
    Registered User C_Coder's Avatar
    Join Date
    Oct 2001
    Posts
    522
    Mmmmm you still have 2 fopen statements, Im sure that wont work remove the first one. as for not displaying the contents of the file you need to add a line in your loop to print them to the screen.
    Code:
    while(fgets(Name,30,fp) != NULL){ 
         printf("%s", Name);
    }
    All spelling mistakes, syntatical errors and stupid comments are intentional.

  5. #5
    Unregistered
    Guest
    Thanx C_Coder it works i didnt noticed that i wrote fopen twice..ohh am glad

    sorry man I have one more question if you dont mind ! :-).

    what's the best way of starting this program to make it read the ID and generate a dynamic linked list, where each node of the linked list would contain a single integer from the ID.



    Thanx again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. read Causes Freezing?
    By Masna in forum C Programming
    Replies: 5
    Last Post: 07-18-2008, 04:31 PM
  2. Read() problems
    By yay4rei in forum C Programming
    Replies: 2
    Last Post: 07-21-2005, 10:47 AM
  3. How can I know the actual bytes read in a file read
    By pliang in forum C++ Programming
    Replies: 1
    Last Post: 06-08-2005, 04:23 PM
  4. What Would You Use To Read User Input?
    By djwicks in forum C Programming
    Replies: 11
    Last Post: 04-05-2005, 03:32 PM
  5. Read Array pro!!Plz help!!
    By Supra in forum C Programming
    Replies: 2
    Last Post: 03-04-2002, 03:49 PM