Dec 14, 2011 at 9:53am UTC
Hi everyone,
I have question regarding parsing char* between function. I speak in program will more easy to understand.
//============
file.h
------
char *Name[1];
char A;
file.cpp
--------
main()
{
#include 'file.h'
functionA;
functionB;
}
functionA
{
Name[0]=A;
}
functionB
{
cout<<Name[0];
}
//==================
The process is in functionA but the display is in functionB, is it possible to parse char* between function?
Thanks.
BR,
Dan
Dec 15, 2011 at 1:00am UTC
It does not work because I am trying to use char array. the error show me can not convert from char[260] to char. need help.
void foo(char* p) {
char file[FILENAME_MAX];
ifstream inFile;
inFile.getline(file,FILENAME_MAX, ',');
p[0] = file;
}
void bar(char* q) {
cout << q[0];
}
int main() {
char* ptr = new char[10];
foo(ptr);
bar(ptr);
}
is it possible to do so? any other suggestion?
Thanks.
Dec 15, 2011 at 6:31am UTC
p[0] = file;
says that you want the first character in whatever string p points to to be the file array. You probably want strcpy(p, file);
Dec 15, 2011 at 9:50am UTC
void foo(char* p);
void bar(char* q);
void foo(char* p)
{
char file[FILENAME_MAX];
ifstream inFile;
do{
cout<<"\nPlease enter a file name.";
cin.getline(file, FILENAME_MAX, '\n');
}while(file[0]==NULL);
cout<<"\nOpening file: "<<file;
inFile.getline(file,FILENAME_MAX, ',');
strcpy(p, file);
//p[0] = file;
}
void bar(char* q)
{
cout << q[0];
}
int main() {
char* ptr = new char[10];
foo(ptr);
bar(ptr);
}
//==================
Do you mean this? this does not display anything words. Please advise. Thanks.
Dec 15, 2011 at 2:23pm UTC
In bar(), q is a string and q[0] is its first character, so there is no way you'd see a word there. Other than that, if you have some problems, maybe the fie is to blame.
By the way, condition file[0]==NULL is never going to be true (unless there is a special key combination to enter null character..).
Dec 16, 2011 at 1:42am UTC
my file content is looks like this:
group,input1,input2,input3,
groupA,1,2,3,
so my target is to read data from file and save into array so that i can call the array to display the data anytime. so my code is looks like this:
//================================
void read(char* p);
void print(char* q);
void read(char* p)
{
char file[FILENAME_MAX];
char name[FILENAME_MAX];
ifstream inFile;
int cnt=0;
do{
cout<<"\nPlease enter a file name.";
cin.getline(file, FILENAME_MAX, '\n');
}while(file[0]==NULL);
cout<<"\nOpening file: "<<file;
for(int i=0; i<8; i++)
{
inFile.getline(name,FILENAME_MAX, ',');
p[cnt]=name;
cnt++;
}
}
void bar(char* q)
{
cout <<"\n"<< q[0];
cout <<"\n"<< q[2];
cout <<"\n"<< q[3];
cout <<"\n"<< q[4];
cout <<"\n"<< q[5];
cout <<"\n"<< q[6];
cout <<"\n"<< q[7];
cout <<"\n"<< q[0];
cout <<"\n"<< q[4];
cout <<"\n"<< q[5];
cout <<"\n"<< q[6];
}
int main() {
char* ptr = new char[10];
foo(ptr);
bar(ptr);
}
//======================
expected display is:
group
input1
input2
input3
groupA
1
2
3
group
groupA
1
2
//=======================
above is my idea or any suggestion to do this? Please advide. I have been try for many method but it still cant display the output. Thanks.
Last edited on Dec 16, 2011 at 1:45am UTC
Dec 16, 2011 at 6:52am UTC
Again, p[n] is a character. You cannot assign an array to it and you cannot print it and expect to get more than one character.
Compile this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
#include <iostream>
#include <fstream>
using namespace std;
void foo(char * p) {
char file[FILENAME_MAX];
ifstream inFile;
cout<<"\nPlease enter a file name." ;
cin.getline(file, FILENAME_MAX, '\n' );
inFile.open(file);
cout<<"\nOpening file: " <<file;
inFile.getline(p,FILENAME_MAX, ',' );
inFile.close();
}
void bar(char * q) {
cout <<"\n" << q;
}
int main() {
char * ptr = new char [FILENAME_MAX];
foo(ptr);
bar(ptr);
}
And see what you get. It should be the first line of your fie.
To divide the string, you can use strtok.
You'll need to use a char** then.
And when you post code, do use [code] tags.
Last edited on Dec 19, 2011 at 2:20pm UTC
Dec 19, 2011 at 12:08am UTC
Hi, i had try the code, there is nothing to display out. :(
Dec 19, 2011 at 2:19pm UTC
whoops. I didn't open the file or even include fstream.. I just noticed that neither did you.. (should be good now)
Last edited on Dec 19, 2011 at 2:21pm UTC