Thread: Creating URL links

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    104

    Creating URL links

    I have a text file with a bunch of links in it. I want to be able to create URL links for each one so I could put them in my bookmarks. The name for each link would be the link, with the special chars taken out. This is what I got.
    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <string.h>
    
    int main (void) {
    FILE *indata=fopen("C:\\windows\\desktop\\urls.txt","r");
    FILE *outdata;
    
       int i;
       char *ext=".url",
    	name[256],
    	url[256];
    
       while (indata!=NULL) {
          fscanf (indata,"%s", url);
          name[0]='"';
          strcat(name,url);
          for (i=0; i<strlen(name); i++) {
    	 if ( (name[i]=='\\') || (name[i]=='/') || (name[i]==':') || (name[i]=='*') || (name[i]=='?') || (name[i]=='"') || (name[i]=='<') || (name[i]=='>') || (name[i]=='|') )
    	    name[i]='-';}
          strcat(name,ext);
          outdata=fopen(name,"w");
          fprintf (outdata,"[InternetShortcut]\n");
          fprintf (outdata,"URL=%s\n",url);
          fclose(outdata);
       }
    fclose(indata);
    return 0;

  2. #2
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Alright... are you having problems? Why are you posting this?

    Also, you shouldn't remove the '/', ':' and '?' characters, because you're quite likely to have a URI like http://www.google.com/search?q=stuff
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  3. #3
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Also, you shouldn't remove the '/', ':' and '?' characters...

    He is replacing these characters to make a valid file name.

    Code:
          name[0]='"';
          strcat(name,url);
    This will set the first character of name to a double quote. You probably want to change these lines to:
    Code:
          strcpy(name,url);
    Code:
       while (indata!=NULL) {
    indata will never equal NULL. You need to read this faq entry.

    It is a good idea to check the return value of functions such as fopen. It is not a good idea to use strlen as a loop condition as it is evaluated every iteration.

    Have fun.

  4. #4
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    >>He is replacing these characters to make a valid file name.
    Whoops, I thought he was replacing those in the URI.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  5. #5
    Registered User
    Join Date
    Oct 2003
    Posts
    104
    Why am I posting it? cause it's not working (running) at all. and I don't know why.

  6. #6
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Originally posted by Hexxx
    Why am I posting it? cause it's not working (running) at all. and I don't know why.
    So far I've seen you post code and a nebulous description of what you are trying to do, hence XSquared's confusion.

    How about finishing your description with an example of your input and expected output.

    Also, take your 2000 character if statement and make it an isFname() function for readability.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  7. #7
    Registered User
    Join Date
    Oct 2003
    Posts
    104
    I did say what it is suppose to do, refering to both the input and output. Okay then... there is a file called urls.txt In it has a url on each line. Like this
    Code:
    http://www.google.com
    www.fraggle.com/ggg
    www.various.org
    http://fffff.com
    ftp://///blkahlacj.com
    Now I'm sure you all know you can't click on them in text form, or go though a few hudred and add them to your bookmarks maually. All a bookmark actually is a a plaintext file with the format
    Code:
    [InternetShortcut]
    URL=http://www.google.com
    The name of the plaintext "file" I want created should have the name of the url so I can know what the link is too. Like "http--google.com.url"

    I would change the if statement to a function eventually, I just want to make sure it actually works, and creates a URL link for each line in the input file.

    Does that makes more sense?

  8. #8
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    When you say it doesn't do anything... no output files are created? Is it opening the input file correctly? Throw some assert( )s in there.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  9. #9
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <string.h>
    int main (void)
    {
      FILE *indata;
      FILE *outdata;
      int i;
      char  ext[]=".url",name[256],url[256];
      
      if ( (indata=fopen("urls.txt","r")) == NULL)
      {
        puts("Unable to open input file.  Exiting.");
        exit(1);
      }
      
      while (fgets(url, sizeof(url), indata) != NULL) 
       {
          if (url[strlen(url)-1]=='\n')
            url[strlen(url)-1]='\0';
          name[0]='"';
          strcat(name,url);
          for (i=0; i<strlen(name); i++) 
          {
            if ( (name[i]=='\\') || (name[i]=='/') || (name[i]==':') || (name[i]=='*') || 
                 (name[i]=='?') || (name[i]=='"') || (name[i]=='<') || (name[i]=='>') || (name[i]=='|') )
               name[i]='-';
          }
          strcat(name,ext);
          if ( (outdata=fopen(name,"w")) == NULL)
            printf("Unable to open file %s.\n",name);
          else
          {
            printf("File %s opened.\n", name);
            fprintf (outdata,"[InternetShortcut]\n");
            fprintf (outdata,"URL=%s\n",url);
            fclose(outdata);
          }
          for (i=0; i<sizeof(name); ++i) /* Clean up the name */
            name[i]='\0';
       }
    fclose(indata);
    return 0;
    }
    Always, always, ALWAYS , do error checking when dealing with I/O

    edit: Made a minor change. Also realized I meant to mention that you need to learn to use proper indentation.
    Last edited by Thantos; 12-30-2003 at 03:39 PM.

  10. #10
    Registered User
    Join Date
    Oct 2003
    Posts
    104
    what's wrong with my indentation?

  11. #11
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >what's wrong with my indentation?
    It isn't consistent, nor is your use of whitespace between tokens.
    My best code is written with the delete key.

  12. #12
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Here is a point to consider regarding the buffer sizes:
    Code:
    char  ext[]=".url",name[256],url[256];
    Instead of arbitrarily choosing buffer sizes, why not use FILENAME_MAX? (FILENAME_MAX is a macro "which expands to an integer constant expression that is the size needed for an array of char large enough to hold the longest file name string that the implementation guarantees can be opened".) Perhaps something like this.
    Code:
    char        name [ FILENAME_MAX ];
    const char  ext[] = ".url";
    char        url [ sizeof name - (sizeof ext - 1) ];
    Note that the size of 'url' in the above declaration does not make room for the opening double-quote (that appears to lack a corresponding closing double quote) in 'name'.

    Also, what will you do if the link is longer than the buffer size you provide? As coded, I believe it would split the string and create two (or more) non-links as links.
    Last edited by Dave_Sinkula; 12-31-2003 at 02:09 PM.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating a map
    By DanFraser in forum C# Programming
    Replies: 7
    Last Post: 01-23-2009, 06:23 AM
  2. Profiler Valgrind
    By afflictedd2 in forum C++ Programming
    Replies: 4
    Last Post: 07-18-2008, 09:38 AM
  3. Interpreter.c
    By moussa in forum C Programming
    Replies: 4
    Last Post: 05-28-2008, 05:59 PM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM