fscanf issues

This is a discussion on fscanf issues within the C Programming forums, part of the General Programming Boards category; Hi all, I have created a code snippet to read data from file using fscanf. Here is the code: data.h ...

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    1

    Lightbulb fscanf issues

    Hi all,

    I have created a code snippet to read data from file using fscanf.

    Here is the code:

    data.h


    Code:
     #include<stdio.h>
      #include<stdlib.h>
      int SendDataIn(); 


    data.cpp
    Code:
     #include “data.h” 
      int SendDataIn() 
      { 
      printf("\n entered \n"); 
      return SUCCESS; 
      } 


    main.cpp

    Code:
     #include “data.h” 
      int main() 
      { 
      char *filename="input.txt"; 
      int mode=0; 
      char opcode[8]; 
      FILE *filepointer; 
      int linecount = 0; 
      int return_value = SUCCESS; 
      char cFile[80]; 
      char *isTxtFile=".txt"; 
      char *isMatch; 
      int j = 0; 
      int limit =8; 
      return_value = fopen_s(&filepointer,filename, "r"); 
      if(return_value != SUCCESS){ 
      printf("\nERROR:Failed to open the file"); 
      return return_value; 
      } 
      
      while(feof(filepointer)== 0){ 
      
      if(mode == 0){ 
      return_value = fscanf_s(filepointer,"%d",&mode); 
      
      } 
      j=0; 
      memset(opcode,0,8); 
      while(j<8){ 
      return_value=fscanf_s(filepointer,"%x",&opcode[j]); 
      if(return_value <= 0){ 
      printf("\nll FSCANF ERROR IN LINE :%d",(linecount+1)); 
    
      return FAILURE; 
      } 
      j++; 
      } 
      fscanf_s(filepointer,"%s\n",cFile); 
      
      if(return_value < 0){ 
      printf("\ngg FSCANF ERROR"); 
    
      return FAILURE; 
      } 
      
      isMatch = strstr(cFile, isTxtFile); 
      if(isMatch == NULL){ 
      if(mode !=1){ 
      printf("\nkk FSCANF ERROR\n"); 
    
      return FAILURE; 
      } 
      } 
      printf("Issuing opcode %x in  mode %d cFile %s",opcode[0],mode,cFile);     
      if(mode == 2){ 
      
      //return_value=SendDataIn(); 
      if(return_value != SUCCESS){ 
      printf("\n not success \n"); 
      return return_value; 
      }else{ 
      printf("\n COMMAND EXECUTION COMPLETED SUCCESSFULLY\n"); 
    
      } 
      } 
      
      
       linecount++; 
      } 
      }
    



    -------
    input.txt
    ---------

    01 e8 00 00 00 00 00 01 01 gg.txt
    02 ff 02 04 06 08 09 01 0c test.txt
    01 ec 02 03 04 05 06 09 0b kk.txt


    My question is:
    if i have the fuction SendDataIn()in same .cpp file say in main.cpp,i am able to retreive the data correctly..

    output:

    Issuing opcode e8 in mode 01 cFile gg.txt
    Issuing opcode ff in mode 02 cFile test.txt
    entered
    COMMAND EXECUTION COMPLETED SUCCESSFULLY
    Issuing opcode ec in mode 01 cFile kk.txt

    if the SendDataIn() is in another .cpp say data.cpp,i am getting the the output as:

    Issuing opcode e8 in mode 01 cFile
    ll FSCANF ERROR IN LINE:2


    why so?.. when i print the values of opcode and mode ,its all zeroes.whether any mistakes is in my code?..can anyone give me some suggestions on using fscanf.

  2. #2
    Registered User
    Join Date
    Aug 2010
    Posts
    231
    Why do you use a cpp file for C?
    Do you use a C++ Compiler?
    You should never use feof before any read-action for a stream.
    Indent your code, its horrible.

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,801
    Code:
    char *filename="input.txt"; 
    ...
    char *isTxtFile=".txt";
    A nit, but those should be const char *



    Code:
    char cFile[80];
    ...
    fscanf_s(filepointer,"%s\n",cFile);
    You're not following the requirements of the fscanf_s call in regards to the cFile argument. From the MSDN page:
    The main difference between the secure functions (with the _s suffix) and the older functions is that the secure functions require the size of each c, C, s, S and [ type field to be passed as an argument immediately following the variable.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by hk_mp5kpdw View Post
    From the MSDN page:
    The main difference between the secure functions (with the _s suffix) and the older functions is that the secure functions require the size of each c, C, s, S and [ type field to be passed as an argument immediately following the variable.
    Why on earth would %c need a width specifier?

    They're so silly sometimes.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fscanf help
    By snielsen in forum C Programming
    Replies: 16
    Last Post: 09-03-2009, 12:41 PM
  2. fscanf
    By mesmer in forum C Programming
    Replies: 5
    Last Post: 11-09-2008, 09:34 AM
  3. I need help with fscanf
    By thall in forum C Programming
    Replies: 6
    Last Post: 04-13-2007, 09:32 AM
  4. fscanf and EOF
    By .ZG. in forum C Programming
    Replies: 3
    Last Post: 05-30-2004, 07:49 AM
  5. fscanf
    By Unregistered in forum C Programming
    Replies: 5
    Last Post: 04-16-2002, 12:29 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21