Thread: Newbie homework help

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    3
    Thanks for the help so far. I am starting to close in on this but I am still having some trouble. I've changed some things around and can get the program to run as intended when I hard code in a file name, but I am still having problems when I try to use a file name specified in argv[1]. I know it seems like I am just continuing to ask questions until someone gives me the correct code, but I assure you I am only using the forum as a last resort and am only wanting guidance, not the answer. Thanks again in advance.

    Code:
    /**
    	@page lab14 Simple Input and Output
    	This program accepts the name of an input file, reads numbers and operators
    	from the input file and then dispalys the sum of the numbers.
    	@section Authors
    	@author Justin Foss
    	<p>
    	@section Contributors
    	- N/A
    	*/
    	/**
    	@file G03426789.c
    	@version 1.0
    	@details
    	Demonstrate input and output using fscanf and fprintf
    	*/
    	#include <stdio.h>
    	#include <stdlib.h>
    	
    
        
    
    void main_end();                                
    
    
    
    int main(const int argc, const char **argv)
    {
        float val_A, val_B, val_C, sum; //Float variable
        char ch_A;                      //Character variable
        FILE *fp;                       //File pointer
        
        fp = argv[1];
        
         //Displays error message if file does not exist
        if (fp == NULL){
          printf("Not a valid file\n");
          exit(1);
           }
       
       //Scans values from specified file
       else if (fp != NULL){
        fp = fopen(argv[1], "r"); 
        fscanf(fp, "%f %f %f %c", &val_A, &val_B, &val_C, &ch_A);
        sum = val_A + val_B + val_C;
             printf("Read the following values from %s\n", argv[1]);
             printf("\t%.3f\n \t%.3f\n \t%.3f\n", val_A, val_B, val_C);
             printf("\n%.3f + %.3f + %.3f = %.3f", val_A, val_B, val_C, sum);
              
             
    }
      //Closes file
      fclose(fp);
      fgetc(stdin);
    }
                                   
                                                   
    void main_end()
    
    {
          printf("Press ENTER to end");
          fgetc(stdin);                                
          printf("[normal program end]\n");
          exit(0);                                     
    }

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Can you PLEASE enable warnings in your compiler:
    Code:
        fp = argv[1];
    makes about as much sense as
    Fish underpants vacuum-cleaner
    does as a sentence.

    fp is a file-pointer, argv[1] is a pointer to char. They do not match, and certainly setting fp to point to a char pointer is not a good plan for executing correctly. fp needs to point to a FILE object. You get a file-object by calling fopen(). In that respect, your original code was much closer.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. newbie homework help please
    By phnominon in forum C Programming
    Replies: 2
    Last Post: 11-11-2006, 09:32 PM
  2. Newbie: Homework Help
    By soheel in forum C++ Programming
    Replies: 6
    Last Post: 10-04-2005, 10:59 AM
  3. Urgent homework help for NEWBIE
    By Kazasan in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2004, 04:23 PM
  4. Homework
    By kermi3 in forum C Programming
    Replies: 0
    Last Post: 09-10-2001, 01:26 PM