Thread: From Linux to Windows - problem

  1. #1
    Registered User
    Join Date
    Apr 2013
    Posts
    5

    From Linux to Windows - problem

    Hi. I wrote some code like this:
    Code:
    FILE * plik;
    
    
    plik = fopen("plik.in", "r");
    fscanf(plik, "(%d,%d),(%d,%d),(%d,%d),(%d,%d)", &ax,&ay,&bx,&by,&cx,&cy,&dx,&dy);
    fclose(plik);
    and plik.in looks like that:
    (0,0),(2,0),(3,3),(0,2)

    On the Linux platform everything is ok, but the same on windows doesn't work. Have anyone any idea why?

  2. #2
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    Can you show your whole code? What errors are you getting?
    Code:
    while(!asleep) {
       sheep++;
    }

  3. #3
    Registered User
    Join Date
    Apr 2013
    Posts
    5
    There are no errors. I can compile the code but after this, program simply doesn't work. Function fscanf loads strange values.
    Code:
    #include <stdio.h>
    #include <math.h>
    
    
    //po podaniu współrzednch punktu funkcja oblicza długość odcinka
    double oblicz_dlugosc_boku(int wspolrzedna_x, int wspolrzedna_y, int wspolrzedna_x2, int wspolrzedna_y2)
    {
    	//zastosowanie twierdzenia pitagorasa do obliczania długości boków
    	double dlugosc_boku = sqrt(((wspolrzedna_x - wspolrzedna_x2)*(wspolrzedna_x - wspolrzedna_x2)) + ((wspolrzedna_y - wspolrzedna_y2)*(wspolrzedna_y - wspolrzedna_y2)));
    	return dlugosc_boku;
    }
    
    
    double oblicz_pole(double a, double b, double c)
    {
    	//obliczanie obwodu
    	double p = (a+b+c)/2;
    	//wykorzystanie wzoru Herona do obliczenia pola trĂłjkÄ…ta i zaokrÄ…glenie wyniku do 4 miejsca po przecinku 
    	double pole = round(10000 * sqrt(p*(p-a)*(p-b)*(p-c)))/10000;
    	return pole;
    }
    
    
    int main ()
    {
    system("chcp 1250");
    
    
    int ax,ay,bx,by,cx,cy,dx,dy;
    double AC,BD,AB,BC,AD,CD,pABC,pACD,pABD,pBCD;
    
    
    FILE * plik;
    
    
    plik = fopen("czworokat.in", "r");
    //wczytywanie kolejnych współrzednych
    fscanf(plik, "(%d,%d),(%d,%d),(%d,%d),(%d,%d)", &ax,&ay,&bx,&by,&cx,&cy,&dx,&dy);
    ///////////////////////////////////////
    printf("%d,%d,%d,%d,%d,%d,%d,%d\n", ax,ay,bx,by,cx,cy,dx,dy);// look at this values, thats what im talking about
    //////////////////////////////////////
    //zamknięcie strumienia
    fclose(plik);
    
    
    //OBLICZANIE DĹUGOŚĆI PRZEKÄ„TNYCH AC,BD
    AC = oblicz_dlugosc_boku(ax,ay,cx,cy);
    BD = oblicz_dlugosc_boku(dx,dy,bx,by);
    //OBLICZANIE DĹUGOĹšCI BOKĂ“W AB,BC,CD,AD
    AB = oblicz_dlugosc_boku(ax,ay,bx,by);
    BC = oblicz_dlugosc_boku(bx,by,cx,cy);
    CD = oblicz_dlugosc_boku(cx,cy,dx,dy);
    AD = oblicz_dlugosc_boku(ax,ay,dx,dy);
    //OBLICZANIE PĂ“L TRĂ“JKÄ„TĂ“W
    pABC = oblicz_pole(AB, BC, AC);
    pACD = oblicz_pole(AC, CD, AD);
    pABD = oblicz_pole(AB, BD, AD);
    pBCD = oblicz_pole(BC, CD, BD);
    
    
    //SPRAWDZANIE CZY POLA SUMY PĂ“L TRĂ“JKÄ„TĂ“W SÄ„ RĂ“WNE
    if((pABC + pACD) == (pABD + pBCD))
    {
    	printf("TAK! Ten czworokąt jest wypukły!\n");
    }
    else
    {
    	printf("NIE! To nie jest czworokąt wypukły!\n %d", ax);
    }
    int i;
    scanf("%d", &i);
    return 0;
    }

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    What does this do "system("chcp 1250");"?
    Is there any reason to believe it does the same thing in Windows?

    Edit: From this site, it appears to be a valid Windows command; but, not sure it does what is needed.
    windows XP command line encoding - Super User

    Tim S.
    Last edited by stahta01; 04-03-2013 at 07:34 PM.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  5. #5
    Registered User
    Join Date
    Apr 2013
    Posts
    5
    Command system("chcp 1250") is for Polish chars in console. I added it on Windows, but it didnt make any difference.

    Any thoughts what is with this miserable fscanf? ;<

  6. #6
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    I suggest checking the return value of fopen to see if it failed.
    Then, checking the return value of fscanf.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  7. #7
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    You should check to see if fopen did not return NULL before using you open file.

    Maybe it might be worth using fgets with sscanf, so that you can see what is happening.
    Fact - Beethoven wrote his first symphony in C

  8. #8
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    I might also have something to do with the format that your text document is in - Someone might be able to fill in whether you need wide char functions
    Fact - Beethoven wrote his first symphony in C

  9. #9
    Registered User
    Join Date
    Apr 2013
    Posts
    5
    Already done that for fscanf. Result is pretty interesting. In place of ints from (0,0),(2,0),(3,3),(0,2) I have From Linux to Windows - problem-s0wlfb-jpg in 2nd line is false result from if.

    How to check return value of fopen ?

  10. #10
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    if (plik == NULL) -> Failed.
    Fact - Beethoven wrote his first symphony in C

  11. #11
    Registered User
    Join Date
    Apr 2013
    Posts
    5
    With file is everything ok. For me fscanf is curious, i dont get the result it gives.

  12. #12
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    What is the output of
    Code:
    int n = fscanf(plik, "(%d,%d),(%d,%d),(%d,%d),(%d,%d)", &ax,&ay,&bx,&by,&cx,&cy,&dx,&dy); 
    printf("items read: %d\n", n);
    What is the output of (immediately after opening the file)
    Code:
    int ch;
    while ((ch = getc(plik)) != EOF)
        printf("%c - %d - %x\n", ch, ch, ch);
    Bye, Andreas

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 03-28-2012, 10:10 AM
  2. Problem uder LINUX (gcc) and same code work at Windows (DevC++)
    By miroslavgojic in forum C Programming
    Replies: 5
    Last Post: 11-24-2010, 10:05 AM
  3. snprintf for Linux ok for Windows a problem!!
    By mynickmynick in forum C Programming
    Replies: 9
    Last Post: 06-17-2010, 09:08 AM
  4. Replies: 1
    Last Post: 10-18-2005, 10:20 AM
  5. C++ in Windows or Linux?
    By Machewy in forum C++ Programming
    Replies: 3
    Last Post: 04-15-2003, 04:23 PM

Tags for this Thread