-
April 17th, 2009, 09:16 PM
#1
Not displaying anything
Hey everyone I am new to this forum. I have a quite puzzling question, I am working on a piece of code that'll take words from a word list and cout them on the screen. I uploaded the file and everything I am using strtok and its using the \n as its delimiters.
Heres the code-- It did doesn't "cout" anything. Thanks
#include <iostream>
#include <string>
using namespace std;
int func(char * filePath);
int main()
{
char intake[1024] = {'NULL'};
cout << "Enter in path of the file.\n";
cin >> intake;
func(intake);
return 0;
}
int func(char * filePath)
{
FILE * pFile;
char * x;
pFile = fopen(filePath, "r");
while (pFile != NULL)
{
x = strtok(filePath, "\n");
cout << x;
}
return 0;
}
-
April 19th, 2009, 12:02 AM
#2
Re: Not displaying anything
-
April 19th, 2009, 12:51 AM
#3
Re: Not displaying anything
Are you sure it didn't output anything? Maybe it did but it exited before you saw it.
-
April 19th, 2009, 03:27 AM
#4
Re: Not displaying anything
I just ran your program and this is what printed.
Code:
Enter in path of the file.
F:/Html/htmlfiles/graphics3.htm /* <-- I entered a file path */
F:/Html/htmlfiles/graphics3.htmF:/Html/htmlfiles/graphics3.htmF:/Html/htmlfiles/graphics3.htm
F:/Html/htmlfiles/graphics3.htmF:/Html/htmlfiles/graphics3.htmF:/Html/htmlfiles/graphics3.htm
F:/Html/htmlfiles/graphics3.htmF:/Html/htmlfiles/graphics3.htmF:/Html/htmlfiles/graphics3.htm
/* Infinite loop that continues to print the file path. I only let it run for 20 seconds. */
I am not sure if i typed the filepath correctly or not? But the infinite loop doesnt seem right.
Last edited by g.eckert; April 19th, 2009 at 03:30 AM.
Reason: Comments in code.
-
April 22nd, 2009, 11:15 AM
#5
Re: Not displaying anything
You misused strtok. strtok takes a string and a delimiter. It read filePath as a string. I suggest you use fgets and fputs in cstdio.
Last edited by richard_tominez; April 22nd, 2009 at 11:22 AM.
-
April 22nd, 2009, 11:20 AM
#6
Re: Not displaying anything
Code:
char intake[1024] = {'NULL'};
What are you trying to do there?
and here?
Code:
while (pFile != NULL)
{
x = strtok(filePath, "\n");
cout << x;
}
There's nothing there that will change the value of pFile.
Last edited by GCDEF; April 22nd, 2009 at 11:23 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|