Reading files

This is a discussion on Reading files within the C Programming forums, part of the General Programming Boards category; How would I go about reading in each line of a file? I did look at this thread , but ...

  1. #1
    I am the worst best coder Quantrizi's Avatar
    Join Date
    Mar 2002
    Posts
    644

    Reading files

    How would I go about reading in each line of a file?

    I did look at this thread, but it didn't have exactly what I wanted. What I want to do is read in 1 line which has a title, store that in a CString. Read in the next line with the author's name, store that in a CString. Then read in the writing (2 lines below author's name) and store that into a CString.

    I've been searching MSDN's documents for fgets, fseek, and all the other C file functions, but none seem to be what I need. Here's the code I have so far:

    Code:
    void CPoetryEditorDlg::OnOpenFile() 
    {
    	// TODO: Add your control notification handler code here
    	FILE *fp;
    
    	poem.Empty();
    	author.Empty();
    	dt.Empty();
    	title.Empty();
    	pb.Empty();
    
    	fp = fopen(file, "r");
    
    	if(fp == NULL){
    		AfxMessageBox("Unable to open file for reading!");
    		
    		fclose(fp);
    	}
    
    	fseek(fp, 0L, SEEK_SET);
    
    	fscanf(fp, "%s", title);
    	fscanf(fp, "%s", author);
    	fscanf(fp, "%s", dt);
    	fscanf(fp, "%s", pb);
    
    	fclose(fp);
    }
    If someone could even just explain the 0L in the fseek function (I got the fseek & fscan code from MSDN, but it didn't work).

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    I wasn't aware that C's I/O functions worked with CString objects. Perhaps a C-style string used as a buffer would be better suited if you're not going to use MFC I/O:
    Code:
    void CPoetryEditorDlg::OnOpenFile() 
    {
      // TODO: Add your control notification handler code here
      FILE *fp;
      char buf[BUFSIZ];
    
      poem.Empty();
      author.Empty();
      dt.Empty();
      title.Empty();
      pb.Empty();
    
      fp = fopen(file, "r");
    
      if(fp == NULL){
        AfxMessageBox("Unable to open file for reading!");
        return;
      }
    
      //fseek(fp, 0L, SEEK_SET); Not needed, you're at the beginning already.
    
      fscanf(fp, "%s", buf);
      title = buf;
      fscanf(fp, "%s", buf);
      author = buf;
      fscanf(fp, "%s", buf);
      dt = buf;
      fscanf(fp, "%s", buf);
      pb = buf;
    
      fclose(fp);
    }
    My best code is written with the delete key.

  3. #3
    I am the worst best coder Quantrizi's Avatar
    Join Date
    Mar 2002
    Posts
    644
    Thank you Prelude. One more question though, I have the filename specified in a different funtion (same source code though), how would I get that? Because my file pointer never gets the file that it's supposed to get.

    Also, in a CString, how do you create new lines?
    Code:
    CString file;
    file.Format("%s\nasdf");
    doesn't work, and neither does adding a \r before the \n
    Last edited by Quantrizi; 04-14-2004 at 10:02 AM.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >I have the filename specified in a different funtion (same source code though), how would I get that?
    The easiest way would be to make the variable global. If it won't be used outside of that source file, you can make it static (or put it in an unnamed namespace) to make it local to the file but invisible to the rest of the program.

    >Also, in a CString, how do you create new lines?
    To the best of my knowledge, CString is just a wrapper around character arrays. A newline should be treated normally, but I've never used CStrings. Maybe the Windows Programming forumites could help you more on that.
    My best code is written with the delete key.

  5. #5
    I am the worst best coder Quantrizi's Avatar
    Join Date
    Mar 2002
    Posts
    644
    Quote Originally Posted by Prelude
    >I have the filename specified in a different funtion (same source code though), how would I get that?
    The easiest way would be to make the variable global. If it won't be used outside of that source file, you can make it static (or put it in an unnamed namespace) to make it local to the file but invisible to the rest of the program.

    >Also, in a CString, how do you create new lines?
    To the best of my knowledge, CString is just a wrapper around character arrays. A newline should be treated normally, but I've never used CStrings. Maybe the Windows Programming forumites could help you more on that.
    Thank you, that seems to work fine

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading User Defined Files
    By Necrofear in forum C++ Programming
    Replies: 17
    Last Post: 06-30-2006, 12:55 AM
  2. Linking header files, Source files and main program(Accel. C++)
    By Daniel Primed in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2006, 10:46 AM
  3. reading files
    By hiya in forum C++ Programming
    Replies: 7
    Last Post: 05-21-2005, 11:40 AM
  4. A little help reading from files...
    By Invincible in forum C++ Programming
    Replies: 4
    Last Post: 04-03-2002, 09:43 AM
  5. Need Advice in reading files
    By jon in forum C Programming
    Replies: 4
    Last Post: 10-07-2001, 07:27 AM

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