having trouble with string handling

Y

YiMkiE

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;

}
 
R

Richard Bos

YiMkiE said:
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
 
I

Ian Collins

YiMkiE said:
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.
 
Y

YiMkiE

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

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?
 
I

Ian Collins

YiMkiE said:
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?
 
K

Kenneth Brody

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:[email protected]>
 
A

Andrew Poelstra

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).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

C language. work with text 3
URGENT 1
Working with files 1
having trouble with basic multithreading 12
File handling code 4
File Handling (Newbie) 9
error 28
String Matching 4

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,816
Latest member
SapanaCarpetStudio

Latest Threads

Top