am stuck in reading a file

This is a discussion on am stuck in reading a file within the C Programming forums, part of the General Programming Boards category; this is my code: Code: #include <stdio.h>#include <string> using namespace std; int main(){ FILE *file1; // fisrt file int Num_Of_Bays, ...

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

    am stuck in reading a file

    this is my code:

    Code:
    #include <stdio.h>#include <string>
    
    
    using namespace std;
    
    
    
    
    int main(){
        FILE *file1; // fisrt file 
        
        int Num_Of_Bays, Docking_ID, Max_size,Time_Taken;
        double Chargs;
        char Harbour_Name;
        int input_status,i;
        
        
        file1 = fopen("Port_Data_File.txt", "r");        /* Open file first.txt */
        if (file1 == NULL) {
        /* Couldn't open the file. */
        printf("Input file not found.\n");
        return -1; /* Non-zero means error. */
        }
        
    
    
        
        
    
    
        for (i=0;i<Harbour_Name;i++){
        i!=EOF;
        fscanf(file1,"%d%c%d%d%1f", &Num_Of_Bays,&Harbour_Name,&Docking_ID,&Max_size,Time_Taken,&Chargs);    
    
    
        }
           printf("\n");
        printf("NUMBER OF DOCKING BAYS ARE %d HARBOUR NAME IS %c\n",Num_Of_Bays,Harbour_Name);
        printf("\n");
        printf("DOCKING ID   MAXIMUN SIZE OF BAY   TIME TO UNLOAD  CHARGE\n");
        printf("==========   ===================   ==============  ======\n");
        printf("%d%d%f\n", Docking_ID,Max_size,Time_Taken,Chargs);
    
    
    
    
    
    
    
    
    
    
    
    
        
        system("PAUSE");
        return EXIT_SUCCESS;
    }

    this is the txt ( 4 columns):
    notice that there is a space between 2234,10000,20000 and 12.50 ....................ect
    10 Eilat
    2234 10000 20000 12.50
    2235 12400 24800 22.78
    2236 8300 16600 34.55
    2237 11786 12500 14.34
    2238 16901 25023 9.78
    2239 19076 38678 23.06
    2240 14238 24356 127.08
    2241 511 1256 13.50
    2242 45403 85000 137.00
    2243 600 1000 17.86
    Last edited by abood1190; 10-15-2011 at 04:25 AM.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You need to read the first line, since it has its own format. Then you need to read a number, a space, a number, a space, a number, a space, a float.
    Code:
    fscanf( file1, " %d %d %d %f", ... );
    Notice the spaces. If you want to read spaces, then add them in your formatting string. The scanf functions expect exactly formatted input, so you have to tell it exactly how the file is set up.


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

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    38
    i think you mean this
    Code:
    fscanf(file1,"%d %c\n%d %d %1f",&Num_Of_Bays,&Harbour_Name,&Docking_ID,&Max_size,Time_Taken,&Chargs);
    but it didnt work. does my question relate to structures in c programming.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by abood1190 View Post
    i think you mean this
    Nope. I said what I meant.


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

  5. #5
    Registered User
    Join Date
    Oct 2011
    Posts
    38
    then how can i move to the second line after reading the first line


    first line = integer + string
    second line = integer + integer + integer + double
    .
    .
    .
    .
    until the tenth line.

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Read in the first number and string, on the first line. Looks like the first number in the first line, is telling you there will be 10 lines of data to be read, so:

    Read the firstNumber, firstString. Then the following lines with a loop:

    Code:
    for(i=0;i<firstNumber;i++) {
       fscanf(filePointer, "%d %d %d %f%c",&n1,&n2,&n3,&float1,&newline);
    }

  7. #7
    Registered User
    Join Date
    Oct 2011
    Posts
    38
    Looks like the first number in the first line, is telling you there will be 10 lines of data to be read
    YES but
    how can i tell the compiler that i want start from second line from a file ?

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by abood1190 View Post
    YES but
    how can i tell the compiler that i want start from second line from a file ?
    You shouldn't have to. The disk file has it's own pointer that keeps track of where you left off. You simply continue reading and the operating system will track the read position for you. That's why they call them sequential files....

    You read the first line by one scanf() format... then you read the remaining lines in a loop using a different format... it's not hard. Just keep reading.

  9. #9
    Registered User
    Join Date
    Oct 2011
    Posts
    38
    ok .. i did what you said but its still the same

    Code:
        fscanf(file1,"%d %c",&Num_Of_Bays,&Harbour_Name);	for (i=0;i<Harbour_Name;i++){
        i!=EOF;
        fscanf(file1,"%d %d %d %1f",&Docking_ID,&Max_size,&Time_Taken,&Chargs);    
    
    
        }
    it gives me:

    10 E
    3857099680.000000


    it has to be like:
    10 Eilat
    2234 10000 20000 12.50
    2235 12400 24800 22.78
    2236 8300 16600 34.55
    2237 11786 12500 14.34
    2238 16901 25023 9.78
    2239 19076 38678 23.06
    2240 14238 24356 127.08
    2241 511 1256 13.50
    2242 45403 85000 137.00
    2243 600 1000 17.86

  10. #10
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by abood1190 View Post
    ok .. i did what you said but its still the same

    Code:
        fscanf(file1,"%d %c",&Num_Of_Bays,&Harbour_Name);	for (i=0;i<Harbour_Name;i++){
        i!=EOF;
        fscanf(file1,"%d %d %d %1f",&Docking_ID,&Max_size,&Time_Taken,&Chargs);    
    
    
        }
    First of all ... learn how to organize your code for readability.

    Code:
        
    
      fscanf(file1,"%d %c",&Num_Of_Bays,&Harbour_Name);	
      for (i=0;i<Harbour_Name;i++)
        {
          i!=EOF;
          fscanf(file1,"%d %d %d %1f",&Docking_ID,&Max_size,&Time_Taken,&Chargs);    
        }
    Now look at your file format...
    On that first line you have an integer number and a string... not an integer number and a character. Since you are reading only the first character of the string, your file pointer is misplaced and it will read the rest of the file as garbage.

    Also your loop want to exit on the the wrong variable... try using Num_Of_Bays instead of the first letter in the name.

    the i!=EOF; line does exactly nothing.

    In your fscanf() format string you use 1f (one eff), which is an invalid formatting string... you want Chargs to be a float value and you should use simply %f to read it.

    Also from your original code...
    You have two #includes on the same line... the compiler's not going to like that.
    You are also using the C++ keywords "using" and "namespace" both of which should produce syntax errors in C.

  11. #11
    Registered User
    Join Date
    Oct 2011
    Posts
    38
    i fixed everything but it gives me this :10 Eilat22436000.000000----------WAHTS wrong ! i cant post my code here and the i cant make new lines !!!
    Last edited by abood1190; 10-15-2011 at 11:13 AM.

  12. #12
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by abood1190 View Post
    i fixed everything but it gives me this :10 Eilat22436000.000000----------WAHTS wrong ! i cant post my code here and the i cant make new lines !!!
    Why can't you post your code?

    What we really need to see is the entire program so we can give you good advice.

  13. #13
    Registered User
    Join Date
    Oct 2011
    Posts
    38
    it gives me now the tenth line which is :10 Eilat 2243 600 1000 17.86

  14. #14
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by abood1190 View Post
    it gives me now the tenth line which is :10 Eilat2243 600 1000 17.86
    That's because you are reading the whole file into a single variable... each new line clobbers the line before it.

    This leaves you with two choices... process the data inside the loop one line at a time --or-- use arrays to store your data.

  15. #15
    Registered User
    Join Date
    Oct 2011
    Posts
    38
    how can i use arrays ... can you give me hints

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Stuck, can't open file
    By cstruggle in forum C Programming
    Replies: 4
    Last Post: 05-12-2011, 02:39 AM
  2. Replies: 16
    Last Post: 01-04-2007, 02:38 PM
  3. Replies: 6
    Last Post: 10-23-2006, 07:22 PM
  4. stuck with file system ....
    By stalker in forum C++ Programming
    Replies: 3
    Last Post: 10-27-2003, 11:28 AM
  5. Newbie - file i/o - I'm stuck
    By djacko in forum C++ Programming
    Replies: 1
    Last Post: 02-22-2002, 08:56 AM

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