需要帮助将指针内容转换为字符串 [英] need help converting pointer contents into string

查看:51
本文介绍了需要帮助将指针内容转换为字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试编写一个程序来为一堆无线接入点编写配置文件。我从来都不是一个优秀的程序员,并且近十年没有做任何事情因此显然犯了一些错误,我在Windows PC上使用cygwin并且似乎某些函数导致堆栈溢出所以我正在使用fgets而不是fgetc(不知道它解决了问题,但那部分工作)。

我现在的问题是我一次读取几个字符串(计划进行字符串比较,但意识到我想在文件中比较的行不是唯一的)。现在的计划(尽管可能不是正确的方式)是寻找换行/回车并计算线路然后替换我想要的线路(IP地址和远程访问端口)。因为我有很多WAP来编程最终版本只会增加IP和HTTP / SHTTP端口并保存到新文件。因为你会看到我还没有开始最后阶段,但我真的很享受挑战。

导致我在代码中出现问题的行是:& c = compare;

我的想法是将指针数组的内容复制到一个字符串,以便我可以在字符串中搜索/ r或/ n字符(正如你将在它下面的for循环中看到的那样)。

我确定目前它只是创建了原始文件的副本,但是如果有人可以向我解释如何将指针的内容转换为字符串然后我会非常好。我喜欢编码,所以如果你能看到愚蠢的错误,请解释我做错了什么,因为我说很多它已经到位,我希望在我遇到这个障碍后添加功能。

非常感谢和提前感谢

doug


#include< stdio.h>

#include< string.h> ;

#include< stdlib.h>


int main(无效){


int count; / *数量* *

int当前; / *当前文件* /

int ip1; / * ip地址的第一部分* /

int ip2; / * ip地址的第二部分* /

int ip3; / * ip地址的第三部分* /

int ip4; / *第四部分ip地址* /

int IP; / *当前的IP地址* /

char文件[15]; / *初始文件* /

int port; / *远程访问http端口* /

int sport; / *远程访问安全http端口* /

int i; / *整数用于扫描数据读取* /

int line; / *行号* /

char比较[34]; / *比较字符串以查找cr / lf * /


printf(输入WAP数量\ n);

scanf("% d",& count);

printf(输入第一个IP地址,用空格分隔而不是小数点\ n);

scanf("% d%d%d%d"& ip1,& ip2,& ip3,& ip4);

printf("输入文件名,不要放.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 o pened\\\
");


char formatw [] ="%s%d.cfg" ;;

char writeFile [sizeof formatw + 100];

sprintf(writeFile,formatw,file,current);

destinationFile = fopen(writeFile," w");


printf(" file创建\ n");


if(sourceFile == NULL){

printf("错误:无法访问file.c. \ n");

返回1;

}

else if(destinationFile == NULL){

printf(错误:无法创建写入文件。\ n);

返回1;

}

否则{

printf(文件已成功打开。内容:\ n \ nn);

char c [34];


while(fgets(c,34,sourceFile)!= NULL){

/ *保持循环直到空指针... * /

& c =比较;

printf(" String:%s \ n",c);

fputs(c,destinationFile);
/ *一次打印一行文件* /

for(i = 0; I = 34!; i ++){

//检查字符串中的新行字符

printf(" checking string%c\ n",compare [i]);

if(compare [i] =" \r"){

printf(" found it!\ n");

fputc (line,destinationFile);

line ++;

//如果找到换行符,则在行首处输入行号

}

}

}


printf(" text written\\\
);


fclose(sourceFile);

fclose(destinationFile);

}

IP ++;

}

返回0;

}

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;
}

推荐答案

& c =比较......你想做什么?如果你用英文阅读,我认为你试图将c的地址设置为比较中存储的值。是吗?


要复制字符串(在C中)你可以使用strcpy(或者更好,strncpy)

或者如果你试图指出它们到同一个地方,并说比较是一个字符数组。然后将c声明为char * c;并且只是做c =比较;


如果我没有回答你的问题,你能否尝试进一步解释你想要做什么?


谢谢
&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


更改& c到c = compare [i] ...其中i是索引,你应该每次增加它以获得下一个值。


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

raghu


我会在一分钟内尝试一下,我意识到我在那里做了一件非常愚蠢的事:应该是比较= C;因为比较是我试图填充指针c的内容的新变量。会试试看,让你知道我是怎么过的。再次感谢您的建议。
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.


这篇关于需要帮助将指针内容转换为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆