Reading a text file

This is a discussion on Reading a text file within the C Programming forums, part of the General Programming Boards category; Hello guys, I really need your help in here. Please do your best. I have the following: --------------------------------------- Code: Typedef ...

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    5

    Reading a text file

    Hello guys,

    I really need your help in here. Please do your best.

    I have the following:
    ---------------------------------------
    Code:
    Typedef struct textbook
    
    {
    
    char title[70];
    char author[60];
    int year;
    float *price;
    
    } TEXTBOOK;
    ---------------------------------
    and I have the following in my (textbook.txt) file:
    ---------------------------------
    2
    The Big help
    John Green
    1988
    20.78
    Yellow Stone
    Jack Ford
    1912
    19.7

    ----------------------------------

    What I'm trying to do is to create a TEXTBOOK variable and read the data in for each field in that structure. And pass this variable into another function that prints each book with its details in one line. Also, if the first line in the text represent how many are there, so if we have more than 2 the code should get it right.


    Please guys, I'm really stuck in here.
    Last edited by Salem; 03-10-2011 at 07:20 AM. Reason: Restoration

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,175
    Try again without the giant font and I'll take a look.
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    5
    Quote Originally Posted by itsme86 View Post
    Try again without the giant font and I'll take a look.
    I'm using size 3?? is it that bad?

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,175
    Quote Originally Posted by peRFection View Post
    I'm using size 3?? is it that bad?
    The standard font size is preferred. It's what we're all used to and what we read posts in every day. Why would you choose a larger one?

    Anyway, which part of the problem are you stuck on? Do you have code that opens the file? Check out the fopen() function.

    You might want to check this forum's policy on homework. Give it a shot, show us your code, explain which part you're stuck on.
    If you understand what you're doing, you're not learning anything.

  5. #5
    Registered User
    Join Date
    Mar 2011
    Posts
    5
    Quote Originally Posted by itsme86 View Post
    The standard font size is preferred. It's what we're all used to and what we read posts in every day. Why would you choose a larger one?

    Anyway, which part of the problem are you stuck on? Do you have code that opens the file? Check out the fopen() function.

    You might want to check this forum's policy on homework. Give it a shot, show us your code, explain which part you're stuck on.
    Code:
    	  char array[100];
    	  char line[100]; 
    
    
      	  FILE *f;
     	  f = fopen("textbook.txt","r");
    
    		 if(f == NULL)
    
       		 {
           			  printf("Error opening file\n");
          			  return 1;
       		 }
    
    
    
    		for(x=0; x<100; x++)
    		line[x] = '\0';
    
    
    
    		if ( f != NULL )
    		{
    			x = 0;
    
    			while ( fgets ( line, sizeof line, f ) != NULL )
    			{
    				strcpy(array[x], line);
    				x++;
    					/* I'm struggling here, I'm not sure how to store the values of the array into  the original struc variable, and not sure how to pass them by reference to the print_textbook(TEXTBOOK textbook ) function !! */
    			}
    	 	} 
     
     	  	fclose(f);
     	  return 0;
    my comments in the code...

  6. #6
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,175
    Well, the first line you read in should be the number of books in the file, so I would do something like this:
    Code:
    if ( f != NULL)
    {
      // Get the number of books in the file
      fgets(line, sizeof(line), f);
      x = atoi(line);
    
      while(x-- > 0)
      {
        TEXTBOOK book;
        // read the title, author, year, and price lines in from the file and store them in book
        ...
        // Print the book
        print_textbook(book);
      }
    }
    If you understand what you're doing, you're not learning anything.

  7. #7
    Registered User
    Join Date
    Mar 2011
    Posts
    5
    Quote Originally Posted by itsme86 View Post
    Well, the first line you read in should be the number of books in the file, so I would do something like this:
    Code:
    if ( f != NULL)
    {
      // Get the number of books in the file
      fgets(line, sizeof(line), f);
      x = atoi(line);
    
      while(x-- > 0)
      {
        TEXTBOOK book;
        // read the title, author, year, and price lines in from the file and store them in book
        ...
        // Print the book
        print_textbook(book);
      }
    }

    Thanks a lot man, but my problem is the storing.... I'm not that expert and not sure how to store them to the struct variable... do we use for example (f.title) when we call the struct variable because each line represent a single varibale..... Thanks

  8. #8
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,175
    Quote Originally Posted by peRFection View Post
    Thanks a lot man, but my problem is the storing.... I'm not that expert and not sure how to store them to the struct variable... do we use for example (f.title) when we call the struct variable because each line represent a single varibale..... Thanks
    It would be book.member. Like:
    Code:
    fgets(book.title, sizeof(book.title), f);
    fgets(book.author, sizeof(book.author), f);
    fgets(line, sizeof(line), f);
    book.year = atoi(line);
    I'll let you figure out the price one.

    One thing to watch out for: the title and author might have '\n' characters at the end which will make the book information print funny. You should probably get rid of them.
    If you understand what you're doing, you're not learning anything.

  9. #9
    Registered User
    Join Date
    Mar 2011
    Posts
    5
    Quote Originally Posted by itsme86 View Post
    It would be book.member. Like:
    Code:
    fgets(book.title, sizeof(book.title), f);
    fgets(book.author, sizeof(book.author), f);
    fgets(line, sizeof(line), f);
    book.year = atoi(line);
    I'll let you figure out the price one.

    One thing to watch out for: the title and author might have '\n' characters at the end which will make the book information print funny. You should probably get rid of them.
    I have two question, whenever I t try to print the title, author, year....... in the printf("") I gets an error, do I have to do use book.member too in the printf because it isn't working. the second question
    whenever I try to pass the variables to the print_textbook(book) function, do I have to make another variables such as title, author.......... in that function I'm not getting it man, I'm sorry

    Quote Originally Posted by itsme86 View Post
    It would be book.member. Like:
    Code:
    fgets(book.title, sizeof(book.title), f);
    fgets(book.author, sizeof(book.author), f);
    fgets(line, sizeof(line), f);
    book.year = atoi(line);
    I'll let you figure out the price one.

    One thing to watch out for: the title and author might have '\n' characters at the end which will make the book information print funny. You should probably get rid of them.
    it works at the function itself but not when I pass it to the other function.... you're right the new line misses everything I'm trying to get it away but still no hope..... i hope u can help
    Last edited by peRFection; 03-10-2011 at 12:17 AM.

  10. #10
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,806
    I formally request smiting of peRFection from the gods(mods) of this message board for deleting/editing his original post.

    Don't do that!!!

    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,834
    Post restored. Please don't delete your posts, it defeats the whole purpose of the forum.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 04:53 AM
  2. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 05:14 AM
  3. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 02:19 PM
  4. Poblem reading struct in a file
    By The_Kingpin in forum C Programming
    Replies: 1
    Last Post: 10-24-2004, 08:02 PM
  5. Replies: 5
    Last Post: 02-01-2003, 10:58 AM

Tags for this Thread


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