need help converting pointer contents into string

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • blunt
    New Member
    • Nov 2008
    • 11

    need help converting pointer contents into string

    trying to write a program to write the configuration files for a load of wireless access points. i've never been a good programmer and haven't done any for nearly a decade so have obviously made some mistakes, i'm using cygwin on a windows pc and it seems that some functions cause stack overflows so am using fgets instead of fgetc (don't know y it solved the problem but that part is working).
    my problem now is that i am reading strings a few characters at a time (was planning to do a string compare but realised that the lines i want to compare in the file are not unique). the plan now (although am probably not doing it the right way) is to look for line feeds/carriage returns and count the lines then replace the lines that i want (ip addresses and remote access ports). as i have a lot of WAPs to program the final version will just increment IPs and HTTP/SHTTP ports and save to a new file. as you will see i haven't started the last stage yet but am really enjoying the chanllenge.
    the line that is causing me problems in the code is: &c=compare;
    my idea is to copy the contents of the pointer array to a string so that i can search the string for /r or /n characters (as u will see in the for loop below it).
    i'm sure there's loads of redundant code at the moment it just creates copies of the original file but if someone could explain to me how i can convert the contents of the pointer into a string then i would be very greatfull. i like coding so if u can see stupid errors please explain what i have done wrong as i say a lot of it is in place for the functionallity i want to add once i have got past this hurdle.
    much love and thanks in advance
    doug

    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>

    int main(void) {

    int count; /* number of waps */
    int current;/* current file */
    int ip1; /* first part of ip address */
    int ip2; /* second part of ip address */
    int ip3; /* third part of ip address */
    int ip4; /* forth part of ip address */
    int IP; /* current ip address */
    char file[15];/* initial file */
    int port; /* remote access http port */
    int sport; /* remote access secure http port */
    int i; /* integer for scanning data read */
    int line; /* line number */
    char compare[34]; /* comparing string to find cr/lf */

    printf("Enter number of WAPs \n");
    scanf("%d",&cou nt);
    printf("Enter first IP address, separated with spaces not decimal points \n");
    scanf("%d %d %d %d",&ip1,&ip2,& ip3,&ip4);
    printf("Enter file name, do not put .cfg \n");
    scanf("%s",file );

    IP = ip4;
    line = 1;

    for(current = 1; current!=count + 1; current++) { /* loop untill current = count */

    port = 9000 + IP;
    sport = 8000 + IP;

    FILE *sourceFile;
    FILE *destinationFil e;

    char formatr[] = "%s.cfg";
    char readFile[sizeof formatr+100];
    sprintf(readFil e,formatr,file) ;
    sourceFile = fopen(readFile, "r");

    printf("file opened\n");

    char formatw[] = "%s%d.cfg";
    char writeFile[sizeof formatw+100];
    sprintf(writeFi le,formatw,file ,current);
    destinationFile = fopen(writeFile ,"w");

    printf("file created \n");

    if(sourceFile== NULL) {
    printf("Error: can't access file.c.\n");
    return 1;
    }
    else if(destinationF ile==NULL) {
    printf("Error: can't create file for writing.\n");
    return 1;
    }
    else {
    printf("File opened successfully. Contents:\n\n") ;
    char c[34];

    while(fgets(c, 34, sourceFile)!=NU LL) {
    /* keep looping until NULL pointer... */
    &c=compare;
    printf("String: %s \n", c);
    fputs(c, destinationFile );
    /* print the file one line at a time */
    for(i=0; i!=34; i++){
    //check for new line character in string
    printf("checkin g string %c\n",compare[i]);
    if (compare[i]="\r"){
    printf("found it!\n");
    fputc(line, destinationFile );
    line++;
    //if new line character found enter line number at start of line
    }
    }
    }

    printf("text written\n");

    fclose(sourceFi le);
    fclose(destinat ionFile);
    }
    IP++;
    }
    return 0;
    }
  • vmpstr
    New Member
    • Nov 2008
    • 63

    #2
    &c = compare... what are you trying to do? If you read it in English, I think you are trying to set the address of c to be the value stored in compare. Is that right?

    To copy strings (in C) you can use strcpy (or better yet, strncpy)
    Or if you are trying to point them to the same place, and say compare is a character array. Then declare c as char *c; and just do c = compare;

    If I didn't answer your question, can you try explaining a bit further what it is you are trying to do?

    thanks

    Comment

    • gpraghuram
      Recognized Expert Top Contributor
      • Mar 2007
      • 1275

      #3
      change &c to c=compare[i]...where i is the index and you should increment it every time to get the next value.

      raghu

      Comment

      • blunt
        New Member
        • Nov 2008
        • 11

        #4
        cheerz will give it a go

        i'll try that in a minute i have realised that i have done a really silly thing there: it should be compare=c; as compare is the new variable that i am trying to fill with the contents of the pointer c. will try it tho and let you know how i get on. thanks again for the advise.

        Comment

        • blunt
          New Member
          • Nov 2008
          • 11

          #5
          yay that's sorted out that issue but....

          ok that has resolved the issue of making a copy of the contents of pointer c so i can compare them but now i still have the issue with trying to detect newline/carriage return characters: if (compare[i]=="\r"){
          i am getting the error: warning: comparison between pointer and integer
          i've tried it with and without quotes and with a single =.
          i can't find the code to steal online but everyone must have to search thru a text file for newline characters

          while(fgets(c, 34, sourceFile)!=NU LL) {
          /* keep looping until NULL pointer... */
          strncpy (compare,c,34);
          printf("String: %s \n", c);
          fputs(c, destinationFile );
          printf("compare = %s",compare);
          /* print the file one line at a time */
          for(i=0; i!=34; i++){
          printf("checkin g character %c\n",compare[i]);
          if (compare[i]=="\r"){
          printf("found it!\n");
          fputc(line, destinationFile );
          line++;
          }
          }
          }

          Comment

          • JosAH
            Recognized Expert MVP
            • Mar 2007
            • 11453

            #6
            compare is a pointer to a char (or an array of char) so compare[i] is a single char. You should compare it with a single char; like so:

            Code:
            if (compare[i] == '\r') ...
            kind regards,

            Jos

            Comment

            • blunt
              New Member
              • Nov 2008
              • 11

              #7
              ah ok single not double quotes

              that's stopped the compilation errors. i tested it and \r didn't come up with anything so did it again with \n and it's working now :D
              right now just gonna use this to put line numbers in the file so i can see which lines i need to change then change the data in it and will put the code back up here in case there's anyone interested in creating config files for Colubrius access points then they're welcome to it, or anything where u have a a file and need to make copies of it with changes to specific lines.

              Comment

              • blunt
                New Member
                • Nov 2008
                • 11

                #8
                Noooo!!!!

                was going so well but it is inserting the ascii character represented by the hexidecimal value of the variable line: fputc(line, destinationFile );
                this doesn't make sense to. i know it's going to be really obvious but it's stumped me once again.

                Comment

                • blunt
                  New Member
                  • Nov 2008
                  • 11

                  #9
                  for the above

                  it should be putting the line number as an integer

                  Comment

                  • JosAH
                    Recognized Expert MVP
                    • Mar 2007
                    • 11453

                    #10
                    The fputc() function only emits one single character. Maybe you wanted to use the fprintf() function?

                    kind regards,

                    Jos

                    Comment

                    • blunt
                      New Member
                      • Nov 2008
                      • 11

                      #11
                      ahhh

                      sorry for sounding like a pirate, of course knew i was being a retard and as the variable is an integer not a char it assumed that i ment it to display the ascii reresentation. thanks for that and thanks for the previous solution.

                      Comment

                      • blunt
                        New Member
                        • Nov 2008
                        • 11

                        #12
                        new issue trying to skip ahead 2 lines:

                        Comment

                        Working...