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

having trouble with string handling

P: n/a
I am trying to get the user to input the name of the .csv file they
want to check if it exist and also the name of the text file they want
to store the error if file is not exist. The following is the full
source. I found that every time it generate a .csv file for the error
log, instead of a txt file. What's wrong?

#include <stdio.h>
#include <time.h>
#include <string.h>

int main()
{
int e = 1;
FILE *fp;
char in[9]; //stores the file name for checking
char out[21]; //stores the file name for output in case file is
not found

char AMH[] = "S:\\wmp\\mgt info\\DCD\\SDWAMH\\AMH_TdySales-";

time_t rawtime;
struct tm * timeinfo;
char err[20];

rawtime = time (NULL);
timeinfo = localtime (&rawtime);
strftime(err, 21, "%x %X ", timeinfo);

scanf("%s", in);
scanf("%s", out);

strncat(out, ".txt", 5);

strncat(AMH, in, 8);
strncat(AMH, ".csv", 5);

/* check AMH Report */
if ((fp = fopen(AMH, "r")) == NULL){
fp = fopen(out, "a");
fprintf(fp, "%s %s File not found\n", err, AMH);
fclose(fp);
//printf("%s File not found!\n", AMH);
}

return 0;

}

Jul 7 '06 #1
Share this Question
Share on Google+
7 Replies


P: n/a
"YiMkiE" <yi****@gmail.comwrote:
char in[9]; //stores the file name for checking
char out[21]; //stores the file name for output in case file is
not found

char AMH[] = "S:\\wmp\\mgt info\\DCD\\SDWAMH\\AMH_TdySales-";
scanf("%s", in);
scanf("%s", out);

strncat(out, ".txt", 5);

strncat(AMH, in, 8);
strncat(AMH, ".csv", 5);
The strncat() function does not work how you think it works.

Richard
Jul 7 '06 #2

P: n/a
>
The strncat() function does not work how you think it works.
Then what should I do to achieve my purpose?

Jul 7 '06 #3

P: n/a
YiMkiE wrote:
I am trying to get the user to input the name of the .csv file they
want to check if it exist and also the name of the text file they want
to store the error if file is not exist. The following is the full
source. I found that every time it generate a .csv file for the error
log, instead of a txt file. What's wrong?

#include <stdio.h>
#include <time.h>
#include <string.h>

int main()
{
int e = 1;
FILE *fp;
char in[9]; //stores the file name for checking
char out[21]; //stores the file name for output in case file is
not found

char AMH[] = "S:\\wmp\\mgt info\\DCD\\SDWAMH\\AMH_TdySales-";

time_t rawtime;
struct tm * timeinfo;
char err[20];

rawtime = time (NULL);
timeinfo = localtime (&rawtime);
strftime(err, 21, "%x %X ", timeinfo);

scanf("%s", in);
scanf("%s", out);

strncat(out, ".txt", 5);

strncat(AMH, in, 8);
This overflows AMH.

You have to provide a buffer big enough to accommodate the concatenated
strings.

--
Ian Collins.
Jul 7 '06 #4

P: n/a
You have to provide a buffer big enough to accommodate the concatenated
strings.
Thanks you! Now I can make the error log file. However, the output
looks like this:

07/07/06 15:43:19 @ S:\wmp\mgt
info\DCD\SDWAMH\AMH_TdySales-20060708.csv File not found

with a strange character in between. What's that?

Jul 7 '06 #5

P: n/a
YiMkiE wrote:
>>You have to provide a buffer big enough to accommodate the concatenated
strings.


Thanks you! Now I can make the error log file. However, the output
looks like this:

07/07/06 15:43:19 @ S:\wmp\mgt
info\DCD\SDWAMH\AMH_TdySales-20060708.csv File not found

with a strange character in between. What's that?
err is too small. You are also passing 21 to strftime, which is more
than the current size of err.

Why make it so small?

--
Ian Collins.
Jul 7 '06 #6

P: n/a
YiMkiE wrote:
[...]
char AMH[] = "S:\\wmp\\mgt info\\DCD\\SDWAMH\\AMH_TdySales-";
[...]

<OT mode="Windows-specific">

Aside from the other issues, I'd like to point out a style issue.
IMHO, the above line is unnecessarily obfuscated, as the following
works just as well:

char AMH[] = "S:/wmp/mgt info/DCD/SDWAMH/AMH_TdySales-";

Windows, and even MS-DOS, has always taken both forms of the slash
as a path separator. It is only at the application level where the
application may decide that forward slashes represent command line
flags, rather than part of a filename.

</OT>

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h|
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:Th*************@gmail.com>
Jul 7 '06 #7

P: n/a
On 2006-07-07, YiMkiE <yi****@gmail.comwrote:
>>
The strncat() function does not work how you think it works.

Then what should I do to achieve my purpose?
You need to avoid oversnipping context, and then you need to allocate
enough memory in AMH[] (strncat won't do it for you).

--
Andrew Poelstra <http://www.wpsoftware.net/projects/>
To email me, use "apoelstra" at the above domain.
"You people hate mathematics." -- James Harris
Jul 7 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.