By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,454 Members | 3,209 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,454 IT Pros & Developers. It's quick & easy.

need help converting pointer contents into string

P: 11
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

#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");
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");

IP = ip4;
line = 1;

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

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

FILE *sourceFile;
FILE *destinationFile;

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

printf("file opened\n");

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

printf("file created \n");

if(sourceFile==NULL) {
printf("Error: can't access file.c.\n");
return 1;
else if(destinationFile==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)!=NULL) {
/* keep looping until NULL pointer... */
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("checking string %c\n",compare[i]);
if (compare[i]="\r"){
printf("found it!\n");
fputc(line, destinationFile);
//if new line character found enter line number at start of line

printf("text written\n");

return 0;
Nov 27 '08 #1
Share this Question
Share on Google+
11 Replies

P: 63
&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?

Nov 27 '08 #2

Expert 100+
P: 1,275
change &c to c=compare[i]...where i is the index and you should increment it every time to get the next value.

Nov 28 '08 #3

P: 11
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.
Nov 28 '08 #4

P: 11
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)!=NULL) {
/* 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("checking character %c\n",compare[i]);
if (compare[i]=="\r"){
printf("found it!\n");
fputc(line, destinationFile);
Nov 28 '08 #5

Expert 10K+
P: 11,448
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:

Expand|Select|Wrap|Line Numbers
  1. if (compare[i] == '\r') ...
kind regards,

Nov 28 '08 #6

P: 11
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.
Nov 28 '08 #7

P: 11
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.
Nov 28 '08 #8

P: 11
it should be putting the line number as an integer
Nov 28 '08 #9

Expert 10K+
P: 11,448
The fputc() function only emits one single character. Maybe you wanted to use the fprintf() function?

kind regards,

Nov 28 '08 #10

P: 11
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.
Nov 28 '08 #11

P: 11
new issue trying to skip ahead 2 lines:
Nov 28 '08 #12

Post your reply

Sign in to post your reply or Sign up for a free account.