473,396 Members | 2,011 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

need help converting pointer contents into string

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
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",&count);
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 *destinationFile;

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

printf("file opened\n");

char formatw[] = "%s%d.cfg";
char writeFile[sizeof formatw+100];
sprintf(writeFile,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(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... */
&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("checking 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(sourceFile);
fclose(destinationFile);
}
IP++;
}
return 0;
}
Nov 27 '08 #1
11 4327
vmpstr
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?

thanks
Nov 27 '08 #2
gpraghuram
1,275 Expert 1GB
change &c to c=compare[i]...where i is the index and you should increment it every time to get the next value.

raghu
Nov 28 '08 #3
blunt
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
blunt
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);
line++;
}
}
}
Nov 28 '08 #5
JosAH
11,448 Expert 8TB
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') ...
  2.  
kind regards,

Jos
Nov 28 '08 #6
blunt
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
blunt
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
blunt
11
it should be putting the line number as an integer
Nov 28 '08 #9
JosAH
11,448 Expert 8TB
The fputc() function only emits one single character. Maybe you wanted to use the fprintf() function?

kind regards,

Jos
Nov 28 '08 #10
blunt
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
blunt
11
new issue trying to skip ahead 2 lines:
http://bytes.com/answers/c/856799-an...ng#post3437773
Nov 28 '08 #12

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

Similar topics

5
by: ali | last post by:
Hi, I'm trying to understand the reason for different output on the following codes Code1: #include <iostream.h> int main()
2
by: claire | last post by:
I have a char that i convert to a string as follows m_tHeader.m_sshortname is defined as char; string result = new string(m_tHeader.m_sshortname); The problem is that any '\0' chars in the...
26
by: Martin Jørgensen | last post by:
Hi, I don't understand these errors I get: g++ Persort.cpp Persort.cpp: In function 'int main()': Persort.cpp:43: error: name lookup of 'j' changed for new ISO 'for' scoping Persort.cpp:37:...
8
by: SpreadTooThin | last post by:
Basically I think the problem is in converting from a 32 bit integer to a 16 bit integer. I have two arrays: import array a = array.array('L', ) b = array.array('H', ) b = a
27
by: comp.lang.tcl | last post by:
My TCL proc, XML_GET_ALL_ELEMENT_ATTRS, is supposed to convert an XML file into a TCL list as follows: attr1 {val1} attr2 {val2} ... attrN {valN} This is the TCL code that does this: set...
12
by: steven acer | last post by:
hello, i have a java app that constructs an xml from a specific file format and vice versa. i've been asked to convert it to c++, but im not an expert in c++, actually im mere beginner you can...
6
by: dtschoepe | last post by:
Hi all, Working on homework again... I've got a weird problem, I've been banging my head against the wall on what is causing it. I have a pointer to a typdef named Person. At one point in the...
1
by: dabbakal | last post by:
Hello, am a new member and this is my first posting. C++ is the first progrsaming language am taking and is just for 3 months. Having benefited from lot of posting i decided to join. But currently...
1
by: blunt | last post by:
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.