Thread: Read and display contents from text file

  1. #1
    Registered User
    Join Date
    Feb 2009

    Read and display contents from text file


    Im starting this new thread because my other one was moved to c++ for some reason. Im trying to complete a project in C which allows me to "browse" for my text file, then use the program to read and then display the contents, which will later be used for calculations.

    I dont really know where to start so I will post this code:

    #include <stdio.h>
    #include <stdlib.h>
    void main(int argc, char *argv[])
       FILE *fp;   /* file pointer */
       char ch;
      /* see if correct number of command line arguments */
       if(argc !=3)
          printf("Usage: find &lt;filename&gt; &lt;ch&gt;\n");
    This code is a bit of a bodge of information I have found on the internet. Can anyone tell if this code is useable for what I want to do, and if not can you recommend a better code to allow me to do this?



  2. #2
    Join Date
    Dec 2007
    Yeah, this code doesn't do anything more than check to see if two arguments were passed on the command line. If there weren't two, and only two, arguments on the command line, then it would print the usage statement and exit. If there were only two arguments, then the program might crash (because it's closing a FILE * that is unallocated and could be pointing anywhere), or it might just close quietly if you're lucky.

    This is why cut and paste is not a good way to learn to program.

    Also, it should be int main(int argc, char *argv[]), not void main(int argc, char *argv[]).

  3. #3
    Registered User JustMax's Avatar
    Join Date
    Jan 2009
    you might try something like this:
    	char szFilename[ 256 ];
    	memset( &ofn, 0, sizeof(OPENFILENAME) );  
    	strcpy( szFilename, "*.txt" );                 
    	ofn.lStructSize = sizeof(OPENFILENAME);                
    	ofn.hwndOwner = hDlg;
    	ofn.lpstrFilter = "Text File\0*.txt\0All Files\0*.*\0\0";
    	ofn.lpstrFile = szFilename;
    	ofn.nMaxFile = 256;
    	ofn.lpstrTitle = "Open Text File";
    	ofn.Flags = OFN_FILEMUSTEXIST;
    	ofn.lpstrInitialDir = fullPath;	// Put you initial path fullPath
    	if( ! GetOpenFileName( &ofn ) )                    
    	return( FALSE );
    That should give you an "open file" dialog
    Last edited by JustMax; 02-03-2009 at 03:27 PM.

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, 03:53 AM
  2. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  3. read a text file and display the content into the list box
    By mr_empty in forum Windows Programming
    Replies: 10
    Last Post: 12-18-2007, 12:55 AM
  4. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  5. Replies: 3
    Last Post: 03-04-2005, 02:46 PM