Thread: Need help with files

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    23

    Need help with files

    Code:
    int main()
    {
    	int index, points[6];
    	char input[250], output[250];
       FILE *ifp, *ofp;
    	
    	printf("Which file would you like to read from?\n");
    	scanf("%s", input);
    
    	
    	printf("Which file would you like to write too?\n");
    	scanf("%s", output);
    	
    	
    	
    	//Opens the output file to be written too
    	ifp = fopen(output, "w");	
    	//Opens the input file too be read from
    	ofp = fopen(input, "r");
    	
    
    	return 0;
    	
    }

    This is what I currently have. What my program needs to do is open a file that the user chooses and then go through it and make a calculation. A sample input file looks like this:

    4
    Chris_Webber F 978 447 252 68 34
    Allen_Iverson G 2302 299 597 180 9
    Grant_Hill F 1317 318 220 97 28
    Steve_Francis G 1663 450 547 112 28

    The number of players at the top and then there name, position, and then stats(which I will need to do a calculation with later. How would I go about programming this. I am not asking anyone to hand feed me code, I just want an idea of where to start. Thanks in advance

  2. #2
    Registered User
    Join Date
    Nov 2005
    Posts
    23
    Please Help.

  3. #3
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Use fgets to read each line, then sscanf to get the data from the resulting string.

    By the way, it's a bit confusing to label your Input file descriptor ofp and your Output file descriptor ifp

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    And you should probably close your files with fclose().
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Quote Originally Posted by dwks
    And you should probably close your files with fclose().
    Perhaps, but the standard guarantees that all open streams will be closed on termination.

    The OP's code should check that the calls to fopen were successful, also.

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Perhaps, but the standard guarantees that all open streams will be closed on termination.
    That's why I said "probably". It's a good habit to get into (I've had more than one obscure bug with unclosed filehandles).
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Ressources files
    By mikahell in forum Windows Programming
    Replies: 4
    Last Post: 06-19-2006, 06:50 AM
  2. add source files to embedded VC 4.0
    By George2 in forum C++ Programming
    Replies: 4
    Last Post: 06-13-2006, 03:28 AM
  3. *.cpp and *.h files understanding
    By ElastoManiac in forum C++ Programming
    Replies: 4
    Last Post: 06-11-2006, 04:45 AM
  4. Linking header files, Source files and main program(Accel. C++)
    By Daniel Primed in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2006, 11:46 AM
  5. Multiple Cpp Files
    By w4ck0z in forum C++ Programming
    Replies: 5
    Last Post: 11-14-2005, 02:41 PM