Thread: Reading a text file

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    88

    Reading a text file

    I have previously posted a question about using code to access a file when the actual name of the file is unknown. the entire name of the text file is stored in a structure component at
    file_info[selection].name. I am confused how I should go about opening this file
    that portion of my code looks like this thus far.
    Code:
                    FILE *readfile;
    	char temp;
    	temp = file_info[selection].name;
    			
    	readfile = fopen(temp,"r");
    		if(readfile == NULL)
    		{
    		printf("\nfile could not be opened");
         
    		}
    		else
    		{	
    		printf("\n file was opened");
                                    }
    	fclose(readfile);
    	return 0;
    Can anyone figure out why this won't work? I appreciate your help.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    temp is just a single char. You want a complete string. Assuming that file_info[selection].name is an array or a pointer to a string you can just make temp a pointer and you'll be set:
    Code:
    char *temp;
    Or you could avoid the temporary variable altogether by doing this:
    Code:
    readfile = fopen(file_info[selection].name ,"r");
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    88
    thanks for your help
    I realized my mistake as soon as I posted it.
    Sometimes I get lost when I stare at code for hours on end

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  2. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  3. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  4. Reading Character at a time from a text file
    By Giania in forum C Programming
    Replies: 8
    Last Post: 02-25-2006, 03:17 PM
  5. A bunch of Linker Errors...
    By Junior89 in forum Windows Programming
    Replies: 4
    Last Post: 01-06-2006, 02:59 PM