468,458 Members | 1,820 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,458 developers. It's quick & easy.

having trouble with string handling

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
7 1217
"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
>
The strncat() function does not work how you think it works.
Then what should I do to achieve my purpose?

Jul 7 '06 #3
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
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
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
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
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.

Similar topics

9 posts views Thread by Penn Markham | last post: by
14 posts views Thread by Gregory L. Hansen | last post: by
1 post views Thread by google | last post: by
7 posts views Thread by Adrian Parker | last post: by
1 post views Thread by John Wright | last post: by
reply views Thread by NPC403 | last post: by
reply views Thread by kmladenovski | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.