Thread: reading txt file from the internet website

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    64

    reading txt file from the internet website

    i want to read a txt file on a ftp server in a file in my computer,
    Ftp Library
    i am getting errors and my program crash down.can anyone tell me whats the problem?


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <windows.h>
    #include <conio.h>
    #include <ftplib.h>
    
    int main()
    {
        char* pass;
        char* p="hello";
    
        int x;
        char *path=("ftp://dhagames.my3gb.com/dhagc.txt:21");
        const char *output=("d:/danish.txt");
    
    /*
        printf("Welcome to DHA (Danish,Hassan and Affan) Gaming Zone!\n");
        printf("You have to register First in order to use our Game zone\n");
        printf("\nPress 1 to open our website\n");
        scanf("%d",&x);
        if (x==1)
        {
            system("start firefox.exe www.dhagames.my3gb.com/dha.html");
        }
        */
        netbuf **N;
        FtpInit();
    
    
            if(FtpConnect("74.53.168.164",N))
            {
    
                printf("Connection successful");
                }
            if (FtpGet(output, path,FTPLIB_ASCII,*N))
            {
                printf("Yahoo");
            }
      //  netbuf *n1;
    
    //FtpGet(output,path,FTPLIB_ASCII,n1);
    
    /*    printf("Enter password and press ENTER.\n \n");
    
        {
            printf("Password: ");
            scanf( "%s",  pass);
    
            if(strcmp ( pass, p ) == 0)
            {
                printf("Correct Password. \n \n");
    
            }
            else printf("Wrong Password.Sorry you cant play the game");
    
    }
    */
    }

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    I'm not familiar with this library you're using, but from this page on FtpConnect: FtpConnect it appears that FtpConnect will pass back a pointer to a netbuf object. Than means you have to declare a netbuf pointer, and pass in it's address. The way you have it, you declare a netbuf pointer pointer, which does not allocate space for the netbuf pointer. Try the following (pay careful attention to the * and &):
    Code:
    netbuf *N;
    FtpInit();
    
    if(FtpConnect("74.53.168.164",&N))
    {
        printf("Connection successful");
    }
    if (FtpGet(output, path,FTPLIB_ASCII,N))

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    and since you're already connected to the server - i do not think the path should include the server address part. It seems illogical
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  4. #4
    Registered User
    Join Date
    Nov 2011
    Posts
    64
    it is working perfectly without login but when i use login it crash down.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <windows.h>
    #include <conio.h>
    #include <ftplib.h>
    
    int main()
    {
        int x;
        char *path=("code.shan.pk/dhagc.txt");
        const char *output=("danish.txt");
    
    
        printf("Welcome to DHA (Danish,Hassan and Affan) Gaming Zone!\n");
        printf("You have to register First in order to use our Game zone\n");
        printf("\nPress 1 to open our website\n");
        scanf("%d",&x);
        if (x==1)
        {
            system("start firefox.exe code.shan.pk/");
        }
    
    netbuf *n;
         netbuf **N;
    
    FtpInit();
    
    
    if(FtpConnect("code.shan.pk/",N))
    {
        printf("Connection successful");
    if (FtpLogin("codeshanpk", "project14", n))
    {
        printf("Login successful");
    }
    
    }
    FtpGet(output,path,FTPLIB_ASCII,n);
    }

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Quote Originally Posted by the manual page
    SYNOPSIS

    #include <ftplib.h>
    int FtpConnect(const char *host, netbuf **nControl);

    PARAMETERS

    host
    The name of the host machine to connect to and optionally an alternate port number to use.
    nControl
    The address where the pointer to the newly created control handle should be stored.
    It is NOT sufficient to just go round declaring variables with the right type, and just hope something useful is going to happen.

    Compare these two ideas.
    Code:
    netbuf *n;
    netbuf **N;
    
    FtpInit();
    
    if(FtpConnect("code.shan.pk/",N))
    {
      printf("Connection successful");
      if (FtpLogin("codeshanpk", "project14", n))
      {
          printf("Login successful");
      }
    }
    vs.
    Code:
    netbuf *n=NULL;
    
    FtpInit();
    if(FtpConnect("code.shan.pk/",&n))
    {
      printf("Connection successful");
      if (FtpLogin("codeshanpk", "project14", n))
      {
          printf("Login successful");
      }
    }
    N and &n have the same type (so the compiler is happy), but the code on the receiving end of the function call is going to be deeply disappointed by the garbage pointer which is the result of passing the VALUE of N.

    It NEEDS a pointer to where it is going to store an answer.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    The suggested way of doing the code.
    Code:
    netbuf *n=NULL;
    Your way; do you see the difference; and I am not talking about the fact your variable is uppercase.
    Code:
    netbuf *N;
    I have no idea if this is the reason it is not working; but, I consider setting the pointer to NULL to be required by some functions to work right. This might be one of them. I always try to set my pointers to something valid or to NULL when I declare them.

    Tim S.
    Last edited by stahta01; 01-07-2012 at 10:57 AM.

  7. #7
    Registered User
    Join Date
    Nov 2011
    Posts
    64
    by making netbuf *n=NULL;
    the program crash down again.


  8. #8
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by danishzaidi View Post
    by making netbuf *n=NULL;
    the program crash down again.
    EDIT: Please ONLY use a single variable of N or n; DO NOT USE BOTH!!!!

    Edit: You do realize that FtpConnect returns a value that needs to be checked to see if it works or not.

    From FtpConnect
    If the connection to the remote server if successful, FtpConnect() returns 1. Otherwise, 0 is returned.
    Edit: Glancing at the library code it seems unlikely that you need to allocate the space as I mention below.
    Then I suggest making N point to a valid location.
    The location should be of type netbuf.

    Like below code.
    Code:
    netbuf aBuffer;
    netbuf *n=&aBuffer;
    Tim S.
    Last edited by stahta01; 01-07-2012 at 11:21 AM.

  9. #9
    Registered User
    Join Date
    Nov 2011
    Posts
    64
    no effect.

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Well there's this, to find out what may have gone wrong.
    FtpLastResponse

    Or indeed checking the return results of functions.

    > const char *pathfile="ftp.code.shan.pk/dhagc.txt";
    How about NOT having the full domain name on the front of the file?

    Perhaps use FtpChdir to set the remote 'cwd' to the correct place beforehand?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  11. #11
    Registered User
    Join Date
    Nov 2011
    Posts
    64
    it is not even connecting with the server.wats wrong man
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <windows.h>
    #include <conio.h>
    #include <ftplib.h>
    
    //int main()
    //{
        int main(){
    int a;
    int b;
    int connectED, logIN;
    netbuf *aBuffer;
    netbuf *n=aBuffer;
    char *user="codeshanpk";
    char *password ="pass";
    char * host="ftp.code.shan.pk:21";
    char *outputfile="danish.txt";
    const char *pathfile="/httpdocs/dhagc.txt";
    const char *path="/httpdocs";
    FtpInit();
    b=connectED=FtpConnect(host, &n);
    printf("%d",b);
    logIN=FtpLogin(user, password, n);
    a=FtpChdir(path, n);
    printf("%d",a);
    FtpGet(outputfile,pathfile,FTPLIB_ASCII,n);
    FtpLastResponse(n);
    /*int x;
    printf("Welcome to DHA (Danish,Hassan and Affan) Gaming Zone!\n");
    printf("You have to register First in order to use our Game zone\n");
    printf("\nPress 1 to open our website\n");
    scanf("%d",&x);
    if (x==1)
        {
            system("start firefox.exe code.shan.pk/");
        }
    */
    }

  12. #12
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    What's wrong is you can't program in C, or post a decent error report. "it doesn't work" doesn't help.

    Try copy/pasting your terminal session, with the ACTUAL results of the printouts from your diagnostic.

    > FtpLastResponse(n);
    Now I know you're just ........ing about.
    Did you even READ THE MANUAL!?
    It returns a string - try printing the bloody thing!
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  13. #13
    Registered User
    Join Date
    Nov 2011
    Posts
    64
    Man i tried printing it says no error.

    gethostbyname:no error

    wat does that mean?

    and connection returning 0 value.cant you see i tried printing in the last program i posted.

  14. #14
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    You need to tell us how you're printing out the error. Also, indent your code and fix the system/"firefox.exe" dependancy.

  15. #15
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Suggestion, and this isn't the first time you've been told this I believe, is learn how to program!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. reading txt file from the internet website
    By danishzaidi in forum C Programming
    Replies: 4
    Last Post: 01-03-2012, 12:48 PM
  2. Using data from an internet website
    By danieldelatorre in forum C Programming
    Replies: 3
    Last Post: 10-17-2011, 05:59 PM
  3. Reading from a website?
    By Paul22000 in forum C++ Programming
    Replies: 15
    Last Post: 11-12-2008, 08:12 PM
  4. Reading HTML files and Images from Website
    By Fade_to_Blah in forum C Programming
    Replies: 5
    Last Post: 07-08-2005, 08:49 AM
  5. Reading Files off the internet
    By Unregistered in forum C++ Programming
    Replies: 0
    Last Post: 03-24-2002, 02:18 PM