very bizzare file writing problem


 
Thread Tools Search this Thread
Top Forums Programming very bizzare file writing problem
# 1  
Old 11-26-2008
very bizzare file writing problem

I'm trying to write a function which opens a file pointer and writes one of the function parameters into the file, however for some reason Im getting a core dump error.

The code is as below

Code:
void WriteToFile(char *file_name, char *data)
{
     FILE *fptr;

     /*disk_name_size is a global variable*/

     char str[disk_name_size + 5];
     char sta[(int)strlen(data)];

     strcpy(str,file_name+1);
     strcat(str,".txt");

     strcpy(sta,data);

     /*Open file for appending*/

     fptr = fopen(str,"a");
     
     if(fptr != NULL)
     {
         fprintf(fptr,"%s",sta);
         fclose(fptr);
     }
     



}

I've ensured that the string length of the data passed into sta[] is no more than the num bytes assigned to it, plus all the parameters passed into WriteToFile() are not null.

However, irrgardless of this, it still crashes at fprintf() for some reason that continues to elude me.

Is there something I have missed/misunderstood ?

many thanks
# 2  
Old 11-27-2008
what exactly are you trying to do with the following ?
strcpy(str,file_name+1);

Change it to :
strcpy(str,file_name);
# 3  
Old 11-27-2008
The line char sta[(int)strlen(data) is problematic. When you call strcpy(sta,data) you overflow sta because of the trailing "\0" that strcpy() appends.
# 4  
Old 11-27-2008
The line strcpy(str,file_name+1); has problem

file_name+1 will make the pointer to move forward one char..

change it to strcpy(str,file_name);
# 5  
Old 11-27-2008
Its quite long back I wrote a C program. So please correct me if am wrong.
Can we declare a array's size with a variable?

I meant will following line works ?
char sta[(int)strlen(data)];
# 6  
Old 11-27-2008
It is possible

yes ,it is possible to use variable as size of an array in C programming
# 7  
Old 11-27-2008
Thanks for the info steephen
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bizzare behavior on redirect of stdout

Oracle Linux 5.6 64-bit (derivative of RHEL) Dear Ann Landers, This is about as bizarre as anything I've ever seen. I have a little test script I've been working with. When I redirect stdout to a file, no file. Make a copy of the script to another name. Execute it and redirect stdout, and... (4 Replies)
Discussion started by: edstevens
4 Replies

2. Shell Programming and Scripting

Problem with writing into a file through shell script

Hi all, I have a shell script which I use to login to the server from the client and then from the server I run a bunch of other scripts to complete my task. I am having problems with the script below- #!/bin/bash while read line do connections=`echo $line | cut -d " " -f 1` period=`echo... (3 Replies)
Discussion started by: joydeep4u
3 Replies

3. Programming

Bizzare optimization problem

I have a C program that, for the life of me, I can't see any possible stack corruption in but for some reason corrupts a local variable to 0 when not referenced. My compiler is gcc 4.3.4. But my question's not really a code question, it's a compiler question. The glitch is weirdly specific: ... (3 Replies)
Discussion started by: Corona688
3 Replies

4. UNIX for Advanced & Expert Users

Bizzare TCP/IP problem

Hi all. I have a really really weird problem that I've been working on for days. The problem manifested as users cannot connect to our web servers via SSH when they're using our wireless network. Here's where it gets weird: - Clients from anywhere other than the wireless subnet can... (4 Replies)
Discussion started by: pileofrogs
4 Replies

5. Shell Programming and Scripting

help with counting processes, bizzare behavior

I have a ksh script (dtksh Version M-12/28/93d on Solaris 10) that is run daily by cron and sometime hangs forever. I need to detect if there is an old copy hung before I start the new run, and if so send an email and exit the script. Here is part of the code: #!/usr/dt/bin/dtksh... (4 Replies)
Discussion started by: 73rdUserID
4 Replies

6. IP Networking

Bizzare network attack?

A server I host is having very rare glitches where a file the user downloads will have incorrect contents. This almost never happens when I am looking, I caught it once and only once -- a user messaged me saying his antivirus had given him a warning about an image file downloaded from his... (2 Replies)
Discussion started by: Corona688
2 Replies

7. Shell Programming and Scripting

Problem in writing the data to a file in one row

Hi All I am reading data from the database and writing to temporary file in the below format. 1=XP|external_component|com.adp.meetingalertemail.processing.MeetingAlertEmail|EMAILALERTPUSH|32|4#XP |classpath|/usr/home/dfusr/lib/xalan.jar: /usr/home/dfusr/lib/xerces.jar: ... (2 Replies)
Discussion started by: rajeshorpu
2 Replies

8. UNIX for Advanced & Expert Users

Problem while writing to a file...?

Hi, I have an issue with the file writing... It is, Suppose that if I am writing some data to a file... and at the same time another user has opened the file and want to write in to the file(writing to the file at the same time)...the another has to know that someone has opened the file, mean... (3 Replies)
Discussion started by: vijay4b7
3 Replies

9. UNIX for Dummies Questions & Answers

Problem writing file path to txt file

if test -z "$1" then echo "you must give a filename or filepath" else path=`dirname $1` f_name =`basename $1` if path="." then path=`pwd` fi fi cat $f_name $path >> index.txt The only problem I am encountering with this is writing $path to index.txt Keeps going gaga: cat:... (1 Reply)
Discussion started by: Vintage_hegoog
1 Replies

10. UNIX for Advanced & Expert Users

Bizzare (while statement)

I'm trying to use the while statement to increment a positive number, with a leading "0". when I pass it through, it seems to come out with a negative value, and all the increments remain negative. This is what I have: i=010986294184 j=010986988888 while ; do echo $i i=(($i + 1)) done... (8 Replies)
Discussion started by: Khoomfire
8 Replies
Login or Register to Ask a Question