473,385 Members | 1,824 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

File name gets truncated

jza
I once had a program which would get a directory name in argv[1] and append
"/thumbmap.txt" after it. I couldn't get it to work because when defining some
variables there would appear some garbage in the file name. Already made a
workaround for it, but I still wonder why this does not work as expected.

So, in the following code if I do not define variables cellx .. cgy the program
works fine, but with them defined the file name gets messed up. I wonder why?
Tested under Linux with gcc.

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

int main(int argc, char *argv[]){
char*tmap;
char*thumbmap;
char*themos;
char*small;
char*sortedf;
unsigned long*matrix;
unsigned long long tmaplen;
unsigned long long gopointer;
int mosx,mosy;
unsigned int cellx,celly;
unsigned int smallx,smally;
unsigned int cgx,cgy;
unsigned long cgt;
char sortedfb[23];
unsigned char sortedlen=0;
char sortedserial[9];
unsigned int ddlen=0;
FILE*th;

tmap=(char*)malloc(strnlen(argv[1])+14);
if(tmap==NULL){
printf("Out of memory.\n");
return 1;
}
strcpy(tmap,argv[1]);
strcat(tmap,"/thumbmap.txt");
printf("tmap is %s\n",tmap);
th=fopen(tmap,"r");
if(th==NULL){
printf("Unable to open %s\n",tmap);
return 1;
}
printf("Opened %s\n",tmap);
free(tmap);
fclose(th);
return 0;
}
Nov 14 '05 #1
9 1814

"jza" <jz*@saunalahti.fi> wrote in message
#include <stdio.h>
#include <string.h>

tmap=(char*)malloc(strnlen(argv[1])+14);

Generally I'm sceptical about the benefits of uncast malloc() (the reason
for casting is to allow compilation under C++). However this example shows
that it is indeed possible to forget to include stdlib.h, and I think this
is the source of your problem.
Lose the cast and you will generate the appropriate compiler warning.
Nov 14 '05 #2
jza wrote:

I once had a program which would get a directory name in argv[1]
and append "/thumbmap.txt" after it. I couldn't get it to work
because when defining some variables there would appear some
garbage in the file name. Already made a workaround for it, but
I still wonder why this does not work as expected.

So, in the following code if I do not define variables cellx ..
cgy the program works fine, but with them defined the file name
gets messed up. I wonder why? Tested under Linux with gcc.

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

int main(int argc, char *argv[]){
char*tmap;
char*thumbmap;
char*themos;
char*small;
char*sortedf;
unsigned long*matrix;
unsigned long long tmaplen;
unsigned long long gopointer;
int mosx,mosy;
unsigned int cellx,celly;
unsigned int smallx,smally;
unsigned int cgx,cgy;
unsigned long cgt;
char sortedfb[23];
unsigned char sortedlen=0;
char sortedserial[9];
unsigned int ddlen=0;
FILE*th;

tmap=(char*)malloc(strnlen(argv[1])+14); ^^^^^^^ ^^^^^^^
Don't cast malloc. The cast is hiding your failure to #include
<stdlib.h>, and causing undefined behavior from here on. Also
there is no such function as strnlen().
if(tmap==NULL){
printf("Out of memory.\n");
return 1;
The only values you can return is 0, and if you #include
<stdlib.h> you can also use EXIT_SUCCESS and EXIT_FAILURE.
Anything else is UB.
} *> strcpy(tmap,argv[1]); strcat(tmap,"/thumbmap.txt");
printf("tmap is %s\n",tmap);
th=fopen(tmap,"r");
if(th==NULL){
printf("Unable to open %s\n",tmap);
return 1;
Same comment as above.
}
printf("Opened %s\n",tmap);
free(tmap);
fclose(th);
return 0;
}


You also have a herd of unused variable declarations cluttering
things up to no purpose, and apparently don't know that the blank
shortage is over. It is quite permissable to make your statements
legible with judicious blanks.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 14 '05 #3

"CBFalconer" <cb********@yahoo.com> wrote in message news:40***************@yahoo.com...
jza wrote:


tmap=(char*)malloc(strnlen(argv[1])+14);

^^^^^^^ ^^^^^^^
Also
there is no such function as strnlen().


And if there were, one might want to check how many parameters it takes.
Nov 14 '05 #4
CBFalconer <cb********@yahoo.com> writes:
The only values you can return [from the `main' function] is 0, and if you #include <stdlib.h> you can also use EXIT_SUCCESS and
EXIT_FAILURE. Anything else is UB.


<nitpick>
Actually, it's implementation-defined behavior: see 7.20.4.3#5.
</nitpick>

Martin
--
,--. Martin Dickopp, Dresden, Germany ,= ,-_-. =.
/ ,- ) http://www.zero-based.org/ ((_/)o o(\_))
\ `-' `-'(. .)`-'
`-. Debian, a variant of the GNU operating system. \_/
Nov 14 '05 #5
"CBFalconer" <cb********@yahoo.com> wrote in message
news:40***************@yahoo.com...
jza wrote:
I once had a program which would get a directory name in argv[1]
and append "/thumbmap.txt" after it. I couldn't get it to work
because when defining some variables there would appear some
garbage in the file name. Already made a workaround for it, but
I still wonder why this does not work as expected.
Sounds like you're doing something that messes up the stack.
tmap=(char*)malloc(strnlen(argv[1])+14);

^^^^^^^ ^^^^^^^
... Also there is no such function as strnlen().


Not in standard C, but some implementations (such as the OP's GCC on Linux)
may have one. The problem here is that strnlen should take two arguments
and the OP only passes one, which may be where his stack problem comes from.

FWIW, I tested this code with glibc 2.1.3 and egcs-2.91.66 and didn't get
any corruption (with or without changing strnlen to strlen).

S

--
Stephen Sprunk "Stupid people surround themselves with smart
CCIE #3723 people. Smart people surround themselves with
K5SSS smart people who disagree with them." --Aaron Sorkin

Nov 14 '05 #6
jza wrote:
I once had a program which would get a directory name in argv[1] and append
"/thumbmap.txt" after it. I couldn't get it to work because when defining some
variables there would appear some garbage in the file name. Already made a
workaround for it, but I still wonder why this does not work as expected.

So, in the following code if I do not define variables cellx .. cgy the program
works fine, but with them defined the file name gets messed up. I wonder why?
Tested under Linux with gcc.

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

int main(int argc, char *argv[]){
char*tmap;
char*thumbmap;
char*themos;
char*small;
char*sortedf;
unsigned long*matrix;
unsigned long long tmaplen;
unsigned long long gopointer;
int mosx,mosy;
unsigned int cellx,celly;
unsigned int smallx,smally;
unsigned int cgx,cgy;
unsigned long cgt;
char sortedfb[23];
unsigned char sortedlen=0;
char sortedserial[9];
unsigned int ddlen=0;
FILE*th;

tmap=(char*)malloc(strnlen(argv[1])+14);
if(tmap==NULL){
printf("Out of memory.\n");
return 1;
}
strcpy(tmap,argv[1]);
strcat(tmap,"/thumbmap.txt");
printf("tmap is %s\n",tmap);
th=fopen(tmap,"r");
if(th==NULL){
printf("Unable to open %s\n",tmap);
return 1;
}
printf("Opened %s\n",tmap);
free(tmap);
fclose(th);
return 0;
}

Probably because you abuse malloc() by not #including its
declaration. What is strnlen() ?

/*
In honor of the OP I have named this jza.c
*/

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

int main(int argc, char *argv[])
{
char *tmap;
FILE *th;
int ch;
if (argc > 1) {
tmap = malloc(strlen(argv[1]) + 14);
if (tmap == NULL) {
printf("Out of memory.\n");
return EXIT_FAILURE;
}
strcpy(tmap, argv[1]);
strcat(tmap, "/jza.c");
printf("tmap is %s\n", tmap);
th = fopen(tmap, "r");
if (th == NULL) {
printf("Unable to open %s\n", tmap);
return EXIT_FAILURE;
}
printf("Opened %s\n", tmap);
while ((ch = fgetc(th)) != EOF)
putchar(ch);
free(tmap);
fclose(th);
}
return EXIT_SUCCESS;
}

I could pretty it up a lot more, but then it would be my program and
not yours. :-)
--
Joe Wright mailto:jo********@comcast.net
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Nov 14 '05 #7
In 'comp.lang.c', jz*@saunalahti.fi (jza) wrote:
I once had a program which would get a directory name in argv[1] and
append "/thumbmap.txt" after it. I couldn't get it to work because when
defining some variables there would appear some garbage in the file
name. Already made a workaround for it, but I still wonder why this does
not work as expected.

So, in the following code if I do not define variables cellx .. cgy the
program works fine, but with them defined the file name gets messed up.
I wonder why? Tested under Linux with gcc.

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

int main(int argc, char *argv[]){
char*tmap;
char*thumbmap;
char*themos;
char*small;
char*sortedf;
unsigned long*matrix;
unsigned long long tmaplen;
unsigned long long gopointer;
int mosx,mosy;
unsigned int cellx,celly;
unsigned int smallx,smally;
unsigned int cgx,cgy;
unsigned long cgt;
char sortedfb[23];
unsigned char sortedlen=0;
char sortedserial[9];
unsigned int ddlen=0;
FILE*th;

tmap=(char*)malloc(strnlen(argv[1])+14);
Unknown function. You want strlen(). Please don't retype but copy and paste.

Rather than using this evil cast, you'd better provide a prototype for
malloc() by including <stdlib.h>

if(tmap==NULL){
printf("Out of memory.\n");
return 1;
}
strcpy(tmap,argv[1]);
strcat(tmap,"/thumbmap.txt");
printf("tmap is %s\n",tmap);
th=fopen(tmap,"r");
if(th==NULL){
printf("Unable to open %s\n",tmap);
return 1;
}
printf("Opened %s\n",tmap);
free(tmap);
fclose(th);
return 0;
}


You forgot to check argc.

Try again on these new basis:

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

int main (int argc, char *argv[])
{
int ret = EXIT_SUCCESS;

if (argc > 1)
{
char *tmap = malloc (strlen (argv[1]) + 14);

if (tmap == NULL)
{
printf ("Out of memory.\n");
ret = EXIT_FAILURE;
}
else
{
strcpy (tmap, argv[1]);
strcat (tmap, "/thumbmap.txt");
printf ("tmap is %s\n", tmap);

{
FILE *th = fopen (tmap, "r");

if (th == NULL)
{
printf ("Unable to open %s\n", tmap);
ret = EXIT_FAILURE;
}
else
{
printf ("Opened %s\n", tmap);
fclose (th);
}
}
free (tmap);
}
}
else
{
printf ("usage xx yyy\n");
ret = EXIT_FAILURE;
}
return ret;
}


--
-ed- get my email here: http://marreduspam.com/ad672570
The C-language FAQ: http://www.eskimo.com/~scs/C-faq/top.html
C-reference: http://www.dinkumware.com/manuals/reader.aspx?lib=c99
FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/
Nov 14 '05 #8


jza wrote:
I once had a program which would get a directory name in argv[1] and append
"/thumbmap.txt" after it. I couldn't get it to work because when defining some
variables there would appear some garbage in the file name. Already made a
workaround for it, but I still wonder why this does not work as expected.

So, in the following code if I do not define variables cellx .. cgy the program
works fine, but with them defined the file name gets messed up. I wonder why?
Tested under Linux with gcc.
tmap=(char*)malloc(strnlen(argv[1])+14);
if(tmap==NULL){
printf("Out of memory.\n");
return 1;
}
strcpy(tmap,argv[1]);
strcat(tmap,"/thumbmap.txt");


Maybe they are defining it "\thumbmap.txt"

--
Al Bowers
Tampa, Fl USA
mailto: xa******@myrapidsys.com (remove the x to send email)
http://www.geocities.com/abowers822/

Nov 14 '05 #9
On Sat, 8 May 2004, jza wrote:
I once had a program which would get a directory name in argv[1] and
append "/thumbmap.txt" after it. I couldn't get it to work because when
defining some workaround for it, but I still wonder why this does not
work as expected.

So, in the following code if I do not define variables cellx .. cgy the
program works fine, but with them defined the file name gets messed up.
I wonder why? Tested under Linux with gcc.

#include <stdio.h>
#include <string.h>
You use malloc() and didn't #include <stdlib.h>. This is a problem.
int main(int argc, char *argv[]){
char*tmap;
char*thumbmap;
char*themos;
char*small;
char*sortedf;
unsigned long*matrix;
unsigned long long tmaplen;
unsigned long long gopointer;
int mosx,mosy;
unsigned int cellx,celly;
unsigned int smallx,smally;
unsigned int cgx,cgy;
unsigned long cgt;
char sortedfb[23];
unsigned char sortedlen=0;
char sortedserial[9];
unsigned int ddlen=0;
FILE*th;

tmap=(char*)malloc(strnlen(argv[1])+14);
Don't cast the result of malloc(). This is hiding the fact that you did
not have #include <stdlib.h>.

Second, what is strnlen()? Shouldn't that be strlen()?

Third, way +14? If this is for the "/thumbmap.txt" why not use:

const char extra[] = "/thumbmap.txt";
tmap = malloc(strlen(argv[1])+sizeof extra + 1);

Fourth, what if argv[1] does not exist? Check your inputs.
if(tmap==NULL){
printf("Out of memory.\n");
return 1;
}
strcpy(tmap,argv[1]);
strcat(tmap,"/thumbmap.txt");
printf("tmap is %s\n",tmap);
Everything looks good here. I'd make one change:

strcat(tmap, extra);

I also like to use:

printf("tmap is '%s'\n", tmap);

Not important here but it helps spot things like trailing whitespace.
th=fopen(tmap,"r");
if(th==NULL){
printf("Unable to open %s\n",tmap);
return 1;
}
printf("Opened %s\n",tmap);
free(tmap);
fclose(th);
return 0;
}


--
Send e-mail to: darrell at cs dot toronto dot edu
Don't send e-mail to vi************@whitehouse.gov
Nov 14 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Courtney L. | last post by:
I tried to search for this issue on the group, but don't even know where to start, so here's my problem. We have a very simple form which has a file upload box. Upon submit the file should be...
7
by: Zeljko Knezevic | last post by:
Is it possible to rewrite the entire file (to delete it's content) without knowing the exact path? #include <fstream> void f(std::ostream file) { // ... }
2
by: sp | last post by:
Hello everybody, I have an xml doc and I am trying to write the values from xml file to a tab delimited text file. Currently, what I am doing is I am reading xml file through XpathNavigaotr and...
0
by: David Bray | last post by:
Has anyone encountered (and solved!) this problem? I have an Access database on a commercial ISP. There is an ASP page/script which creates a recordset from the database and then writes a file...
4
by: Jay | last post by:
This is a strange one that I can't seem to find a fix for. We have a Billing DB application (Access 2000 format) where we upload billing info in a comma delimited text file to our printer who...
0
by: Lokkju | last post by:
I am pretty much lost here - I am trying to create a managed c++ wrapper for this dll, so that I can use it from c#/vb.net, however, it does not conform to any standard style of coding I have seen....
7
by: lawrence k | last post by:
I've got a music studio for a client. Their whole studio is run with Macintosh computers. Macintosh computers allow file names to have open white spaces, such as "animal hospital.mp3". I have a...
7
by: elnoire | last post by:
Greetings! I've just started learning python, so this is probably one of those obvious questions newbies ask. Is there any way in python to check if a text file is blank? What I've tried to...
1
by: =?Utf-8?B?Qm9iQWNoZ2lsbA==?= | last post by:
I am using Windows Media Player to play my half second audio files one after the other using the code below... My problem comes when I play a second audio file immediately after the first one...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.