Input files with C++

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kardon33
    New Member
    • May 2007
    • 158

    #1

    Input files with C++

    What i need to do is open a text file in C and output each line into a new array key.
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    What have you tried?

    I can help with specific questions but I cannot be your C/C++ instructor.


    Please read the posting guidelines.

    Comment

    • kardon33
      New Member
      • May 2007
      • 158

      #3
      Code:
      FILE *fp;
      char words;
      char test;
      
      
        fp = fopen ("thanksgiving.txt", "r");
        test = fscanf (fp, "%lf", &words);
        printf("%lf", test);
      
       //  while (test != EOF ){
        //   printf ("%d", words);
      
      //test = fscanf (fp, "%d", &words);
      //}
        fclose (fp);

      Comment

      • oler1s
        Recognized Expert Contributor
        • Aug 2007
        • 671

        #4
        This isn't even a real attempt. The mistakes you made suggest you aren't familiar with the material, or any of the previous concepts required for this problem. If the reason you're having trouble is not knowing the material, then the solution is obvious. Learn C++.

        Here's what makes me think you have no idea what is going on.
        • Use of C code. FILE pointers, fopen, fscanf, fclosef, printf, etc. are all in the C library. You said C++? Did you just copy random code from the internet?
        • Incorrect use of fscanf. %lf is the format specifier for a double. words is a char. This code will probably trash memory. At runtime, you may also see the program segfault here. Doesn't matter though. There's something seriously wrong with that line.
        • You follow the line with a printf statement. Once again, you specify the format as a double. Yet use a char. Furthermore, the char is unitinitialized , leading to garbage.
        • Then you close the file. Eh?
        • Commented out code includes repeated printf statements (huh?) and another guess at how fscanf works.


        Most of your code is nonsense. It's not presented as a compileable format either. There isn't anything to fix here. Fixing means you have something that could be made to work. You don't. Look up your class notes. If you don't have class notes, look at your book, or perhaps look at cprogramming.co m and cplusplus.com . Read the tutorials.

        This will take you time. I hope if this is an assignment with a deadline, you have a long time before the deadline.

        EDIT: I just did a reread. You mention in the title C++. Your post mentions C. Do you even know which language you are using?

        Comment

        Working...