473,804 Members | 3,094 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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\\m gt info\\DCD\\SDWA MH\\AMH_TdySale s-";

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 1437
"YiMkiE" <yi****@gmail.c omwrote:
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\\m gt info\\DCD\\SDWA MH\\AMH_TdySale s-";
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\\m gt info\\DCD\\SDWA MH\\AMH_TdySale s-";

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\\m gt info\\DCD\\SDWA MH\\AMH_TdySale s-";
[...]

<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.c omwrote:
>>
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
4967
by: Penn Markham | last post by:
Hello all, I am writing a script where I need to use the system() function to call htpasswd. I can do this just fine on the command line...works great (see attached file, test.php). When my webserver runs that part of the script (see attached file, snippet.php), though, it doesn't go through. I don't get an error message or anything...it just returns a "1" (whereas it should return a "0") as far as I can tell. I have read the PHP...
2
1873
by: Stephen Horne | last post by:
Just recently I decided I want to make use of my ISP freebie webspace. In order to make that easier, I'd like to be able to automatically synchronise an FTP file/folder heirarchy with one on my local hard drive. I figured this should be easily handled in Python, and broadly speaking it is, but I am having a little difficulty. You see, in order to handle the synchronise correctly, I need to be able to determine what is on the FTP server...
14
2976
by: Gregory L. Hansen | last post by:
I can't seem to make a queue of objects, using the STL queue. I'm trying to make a little event manager, and I just want someplace to store events. The method definitions for EventManager have been commented away to nothing during debugging, but the headers look like class Event { private: Object* recipient; int eventID;
1
1816
by: google | last post by:
It would seem that when I assign an HTML entity to a form text input using "inline" javascript that it will display properly. But when trying to set it via a function call, the entity text shows in the field instead of the symbol it represents. The following example produces these results on Opera 8.5 and IE 6.0 I am expecting the same results whichever way I set the contents of "myText". Why, when choosing "function" does "&copy"...
7
1239
by: Adrian Parker | last post by:
Having a problem with validators. Because we want to only test whether the current field in a changed event is valid before we process the change code, I have tried to turn off all validators for the page (all ctrls are in a table), then just enable the one relevant validator, run the page.validate and then if valid, do the change code. otherwise, skip the change code and then finally enable all the validators again. What's happening...
3
5745
by: Michael | last post by:
Hi all, I'm having trouble PInvoking a TCHAR within a struct. I'll paste the specific struct's API definition below. I've tried so many numerous variations. The main Win32 error I get is 0x3f0 / 515L which amounts to ERROR_NO_TOKEN. Every single instance of this in the past was due to mistakes I made while within PInvoked structs. Is anybody able to point me to documentation or just tell me outright how to
5
3503
by: tkondal | last post by:
Hi all. I just started looking at Python's ctypes lib and I am having trouble using it for a function. For starters, here's my Python code: from ctypes import*; myStringDLL= cdll.LoadLibrary("myStringDLL.dll");
1
2046
by: John Wright | last post by:
I am running a console application that connects to an Access database (8 million rows) and converts it to a text file and then cleans and compacts the database. When it runs I get the following error: The CLR has been unable to transition from COM context 0x1a2008 to COM context 0x1a2178 for 60 seconds. The thread that owns the destination context/apartment is most likely either doing a non pumping wait or processing a very long...
5
13388
matheussousuke
by: matheussousuke | last post by:
Hello, I'm using tiny MCE plugin on my oscommerce and it is inserting my website URL when I use insert image function in the emails. The goal is: Make it send the email with the URL http://mghospedagem.com/images/controlpanel.jpg instead of http://mghospedagem.comhttp://mghospedagem.com/images/controlpanel.jpg As u see, there's the website URL before the image URL.
0
9704
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10558
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10302
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10069
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9130
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7608
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6844
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4277
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2975
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.