fscanf

Discussion in 'C++' started by Andres, Sep 8, 2008.

  1. Andres

    Andres New Member

    Joined:
    Jul 22, 2008
    Messages:
    34
    Likes Received:
    0
    Trophy Points:
    0
    Ok, I have this code using fscanf, basically what I want to do is to send lets say a string "y = " followed by a number. Then I close the file and then reopen it and scan it to take out the string and the number. The problem is that when I scan it, it doesnt give me the input data that I sent. Then what I want to do is to only send my desired data to another file. Can someone help me please?
    Code:
    #include <stdio.h>
    #include <iostream>
    
    
    using namespace std;
    
    int main ()
    {
      int i;
      int x;
      
      char str [80];
      
      FILE * pFile;
      FILE * pFile2;
      FILE * pFile3;
    //Writing to file
      pFile = fopen ("aplus.txt","w+");
      fprintf (pFile, "%s %d", "Y =",30);
      printf ("%s %d\n",str,&i);
      fclose (pFile);
      
      system ("pause");
      //Getting from file
      pFile2 = fopen ("aplus.txt","r");
      rewind (pFile2);
      fscanf (pFile2, "%s", str);
      fscanf (pFile2, "%d", i);
      
     //Writing needed information to file
      pFile3 = fopen ("aplus2.txt","w+");
      fprintf (pFile3, "%d");
      
       fclose (pFile2);
      fclose (pFile3);
      
      printf ("%d and %s \n",&i, str);
      
      system("aplus.txt");
      system("aplus2.txt");
      
      return 0;
      
    }
     
    Last edited by a moderator: Sep 9, 2008
  2. oogabooga

    oogabooga New Member

    Joined:
    Jan 9, 2008
    Messages:
    115
    Likes Received:
    11
    Trophy Points:
    0
    Remember that fscanf needs an address, so if the variable is not already a pointer (like str)
    then you need to put an ampersand in front of it (like &i).
    Also remember that printf does NOT need the ampersand.
    Code:
    #include <stdio.h>
    
    int main () {
      int i;
      char str [80];
      FILE * pFile;
    
      // Writing to file
      pFile = fopen ("aplus.txt", "w+");
      fprintf (pFile, "%s %d", "Y =", 30);
      fclose (pFile);
    
      // Getting from file
      pFile = fopen ("aplus.txt", "r");
      fscanf (pFile, "%s = %d", str, &i);
      fclose (pFile);
    
      printf ("Str: %s  Number: %d\n", str, i);
    
      system("pause");
      return 0;
    }
    
     
  3. Andres

    Andres New Member

    Joined:
    Jul 22, 2008
    Messages:
    34
    Likes Received:
    0
    Trophy Points:
    0
    Thanks oogabooga,
    hey now lets say that I dont create the file "aplus.txt".
    Is it the same procedure if I want to scan another file that already exists.

    For example.
    Lets say theres is a file "whatever.txt" and it has three lines


    Line1 "X = 23"
    Line2 "Y = 34"
    Line3 "Z = 12"

    How would I extract the numbers only to my program?Help please.
     
  4. oogabooga

    oogabooga New Member

    Joined:
    Jan 9, 2008
    Messages:
    115
    Likes Received:
    11
    Trophy Points:
    0
    Absolutely, it should be the same procedure. Give it a try.
    Post again if you have any trouble.
     
  5. Andres

    Andres New Member

    Joined:
    Jul 22, 2008
    Messages:
    34
    Likes Received:
    0
    Trophy Points:
    0
    Thanks it works, is this the right code to use?

    Code:
    #include <stdio.h>
    #include <iostream>
    #include <fstream>
    
    using namespace std;
    
    
    int main () 
    {
      int i;
      int x;
      int y;
      int z;
      
      char str [80];
      FILE * pFile;
      FILE * pFile2;
      
      
      // Creating and Writing to file
      pFile = fopen ("aplus.txt", "w+");
      fprintf (pFile, "%s %d", "X =", 001);
      fprintf (pFile, "\n");
      fprintf (pFile, "%s %d", "Y =", 10000);
      fprintf (pFile, "\n");
      fprintf (pFile, "%s %d", "Z =", 21);
      fclose (pFile);
    
      // Getting from file
      pFile = fopen ("aplus.txt", "r");
      fscanf (pFile, "%s = %d", str, &i);
      x=i;
      fscanf (pFile, "%s = %d", str, &i);
      y=i;
      fscanf (pFile, "%s = %d", str, &i);
      z=i;
      fclose (pFile);
    
      cout<<x<<endl;
      cout<<y<<endl;
      cout<<z<<endl;
      
      //Sending wanted information to another created file
      pFile2= fopen ("aplus2.txt", "w+");
     ofstream file;
     file.open("aplus2.txt");
     file<<x<<endl<<y<<endl<<z<<endl;
     file.close();
     fclose (pFile);
      system("aplus.txt");
      system("aplus2.txt");
      return 0;
    }
     
  6. oogabooga

    oogabooga New Member

    Joined:
    Jan 9, 2008
    Messages:
    115
    Likes Received:
    11
    Trophy Points:
    0
    It looks pretty good. A couple of points:
    1. You don't need to print the newline separately.
    Code:
    So this:
      fprintf (pFile, "%s %d", "X =", 001);
      fprintf (pFile, "\n");
    Can be this:
      fprintf (pFile, "%s %d\n", "X =", 001);
    2. You don't need to read the numbers into the variable i and then transfer them to x, y, and z.
    You can read them directly into x, y, and z like this:
    Code:
    fscanf (pFile, "%s = %d", str, &x);
    fscanf (pFile, "%s = %d", str, &y);
    fscanf (pFile, "%s = %d", str, &z);
    3. You're mixing C and C++ file access techniques. You should probably stick with one
    or the other in the same program. In particular, this code:
    Code:
    pFile2= fopen ("aplus2.txt", "w+");
    ofstream file;
    file.open("aplus2.txt");
    file<<x<<endl<<y<<endl<<z<<endl;
    file.close();
    fclose (pFile); // note that you meant pFile2 here!
    Should be like this:
    Code:
    ofstream file;
    file.open("aplus2.txt");
    file<<x<<endl<<y<<endl<<z<<endl;
    file.close();
    That is, you either use a FILE* pointer and fopen/fclose,
    or you use an ofstream object and obj.open/obj.close.
    There's no point in using both.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice