Thread: Obtaining a txt file through the web

  1. #1
    -
    Join Date
    Feb 2003
    Posts
    47

    Obtaining a txt file through the web

    Hi,
    My problem in a nutshell, i need to fetch a .txt file off the internet, then display the contents.

    My problems are, I dont know what i need to do to acess the internet and fetch a file, the amount of characters in the file can be figured out to an accurate degree.

    My other problem lies in the URL of this txt file i would like to get.
    here it is:
    https://myaccount.optusnet.com.au/my...age.txt?login=[insert user name]&passwd=[insert password]

    Any one australian would most likely know this, and what i need to do (i think) is place the variables into the appropiate places before executing the URL, i hope ive explained my problem in enough detail, below is my code so far, its not much but you guys always seem to ask for it before helping
    so even a link, what functions to use, anything in the right direction is great, thanks.

    Code:
    #include <stdio.h>
    
    int main()
    {
      char username, password;
    
      printf("-\t\t\t\t-\n- Welcome to Optus Netstats.\t-\n- Please enter your" 
             "user name at -\n- the prompt with your password -\n- following.\t\t\t"
             "-\n-\t\t\t\t-\n\n"); 
      
      printf("User Name: \n\n", username);
      /* scanf function? also needs input validation */
      scanf("%s", &username);
      
      printf("Password: \n\n", password);
      scanf("%s", &password);
      
      printf("text %s %s", &username, &password);
      /* we will be fetching a .txt file from 
      https://myaccount.optusnet.com.au/my...age.txt?login=
      [insert user name]&passwd=[insert password]
      the variables from username and password will need to be places into the 
      appropiate places. Then displayed. The amount of characters can be roughly 
      calculated. */
      
      system("PAUSE");	
      return 0;
    }
    P.S. I appologise for the long post and would really appreciate anyone who can spare some time, This is baised on a windows system. Also the code doesnt seem to work as is, maybe something to do with scanf

  2. #2
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    G,day

    Code:
    char username, password;
    This only allocates one character each for username and password. You want something like this:
    Code:
    char username[128], password[128];
    This gives you 128 characters in each.
    Code:
     scanf("%s", &username);
    The correct way to use scanf with a character string is:
    Code:
    scanf("%s", username);
    Notice, no ampersand before username. The same applies with printf.
    You can create a string from multiple parts using sprintf (like printf but the output goes to a string instead of the screen):
    Code:
    char szURL[512]; // at top.
    sprintf(szURL, "https://myaccount.optusnet.com.au/mydatamonitor/usage.txt?login=%s&passwd=%s", username, password);
    Once you've done all that you can put a printf at the bottom to make sure the URL is correct.
    Code:
    printf("%s", szURL);
    Once we've worked out this part and you are getting the correct URL we can move onto the download part of your question.

  3. #3
    -
    Join Date
    Feb 2003
    Posts
    47
    thank you anonytmouse, you have been a great help. I like the australian lingo too

  4. #4
    -
    Join Date
    Feb 2003
    Posts
    47
    here is my new code, if anyone has any more help thats great, if not no worries.

    Code:
    #include <stdio.h>
    
    int main()
    {
      char username[9], /* user names can not exceed higher then 8 characters */
           password[9], /* passwords can not exceed higher then 8 characters */
           URL[512]; /* extra space allowing considering lengths of UN + pass */
    
      printf("-\t\t\t\t-\n- Welcome to Optus Netstats.\t-\n- Please enter your" 
             "user name at -\n- the prompt with your password -\n- following.\t\t\t"
             "-\n-\t\t\t\t-\n\n"); 
      
      printf("User Name: ", username);
        scanf("%s", username);
          fflush(stdin); /* clears extra characters */
      
      printf("\nPassword: ", password);
        scanf("%s", password);
          fflush(stdin); /* clears extra characters */
      
      printf("\ntext %s %s\n\n", username, password); /* stub */
      
      sprintf(URL, "https://myaccount.optusnet.com.au/mydatamonitor/usage.txt?"
                   "login=%s&passwd=%s", username, password); /* puts correct URL
                   to a string for use later */
      puts(URL);
      
      getchar(); /* allows more portability */
      return 0;
    }

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Don't use scanf if you don't want people trashing your buffer. I'm assuming you're doing some CGI here or something similar. In this case you really don't want people running off the end of your buffers. scanf doesn't check for size, it'll read in whatever you give it. If I pipe a 20mb file with no newline characters, it'll happily accept the entire thing in one shot as input, it'll take it and trash whatever memory is past the end of my buffer.

    Buffer overflows are common exploits.

    Use fgets instead.

    Next, you can't use fflush on input streams. It is undefined behavior and is not valid. fflush only works on output streams. Check the FAQ or search the board for fflush(stdin) for more info.

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

  6. #6
    -
    Join Date
    Feb 2003
    Posts
    47
    I only hear bad things about scanf...
    thanks for all your help though...

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Which OS/Compiler are you using Robid?
    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.

  8. #8
    -
    Join Date
    Feb 2003
    Posts
    47
    Windows Xp
    Dev-C++ 4.9.8.0

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 01:04 PM
  3. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  4. Batch file programming
    By year2038bug in forum Tech Board
    Replies: 10
    Last Post: 09-05-2005, 03:30 PM
  5. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM