Thread: File import problem

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    92

    File import problem

    Hello I wrote this program it's meant to take a variablu that is in the value.txt file and it's meant to do a simple calculation then print it's output in the number.txt file. The files are already made in the c directory. I would be highly oblighed if someone can fix my code for me or tell me where the mistakes are.

    Code:
    #include <stdio.h>
    
    int main()
    {
    	float b,c;
    	FILE  *outp, *inp;
    	
    printf(" Please enter the value of B: ");
    scanf("%f",&b);
    
                 inp =fopen("c:value.txt","r");
                 c= b + ;
                 outp=fopen("c:number.txt","w");
    	outp= fprintf(outp, "c is equal to %f \n",c);
    
    fclose(outp);
    fclose(inp);
    return 0;
    }

  2. #2
    Registered User
    Join Date
    Nov 2004
    Location
    USA
    Posts
    516
    Code:
    outp= fprintf(outp, "c is equal to %f \n",c)
    fprintf returns an int, it shouldn't be going into outp. That "outp=" isnt correct.
    also,
    Code:
    c=b+;
    is incorrect..c=b+somevalue.
    Code:
    >+++++++++[<++++++++>-]<.>+++++++[<++++>-]<+.+++++++..+++.[-]>++++++++[<++++>-] <.>+++++++++++[<++++++++>-]<-.--------.+++.------.--------.[-]>++++++++[<++++>- ]<+.[-]++++++++++.

  3. #3
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Aren't you missing a slash in the filenames? It should be more like c:/value.txt or c:\\value.txt
    If you understand what you're doing, you're not learning anything.

  4. #4
    Registered User
    Join Date
    Sep 2005
    Posts
    92

    Still something wrong

    Hello I took all of your seggustions and fixed my code but there is still comething wrong. It seems to have an runtime error. The value of c written in the file is all wrong. Wht could be wrong with it.

    Code:
    #include <stdio.h> 
    
    int main() 
    { 
            int a, b, c; 
            FILE *out, *in; 
    c= a + b;
            in = fopen("C:\\value.txt", "r"); 
            out = fopen("C:\\number.txt", "w"); 
            if(!in || !out) 
            { 
                    printf("Failed to open files\n"); 
                    return 1; 
            } 
            printf("Please enter the value of B: "); 
            scanf("%d", &b); 
            fscanf(in, "%d", &a); 
            fprintf(out, "c is equal to %d\n", c=a+b); 
            fclose(out); 
            fclose(in); 
            return 0; 
    }

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Move the calculation after you have the numbers:
    Code:
    #include <stdio.h>
    
    int main()
    {
       int a, b, c;
       FILE *out, *in;
       in = fopen("value.txt", "r");
       out = fopen("number.txt", "w");
       if ( !in || !out )
       {
          printf("Failed to open files\n");
          return 0;
       }
       printf("Please enter the value of B: ");
       scanf("%d", &b);
       fscanf(in, "%d", &a);
       c= a + b;
       fprintf(out, "c is equal to %d\n", c);
       fclose(out);
       fclose(in);
       return 0;
    }
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  3. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 01:19 PM
  4. File I/O problem
    By 81N4RY_DR460N in forum C++ Programming
    Replies: 12
    Last Post: 09-03-2005, 12:14 PM
  5. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM