473,321 Members | 1,667 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,321 software developers and data experts.

Seg Fault. help please

This is a strage seg fault...i threw some comments in so you can see whats going on. Thanks!
any help would be appreciated.




/************************************************** ****************************
* Function: ReadString
*
* This function reads the entire text file into dynamically allocated memory,
* returns a pointer to the text as it's return value and returns the length
* via the parameter lengthPtr by reference.
*
* Inputs: filename, lengthPtr
* Outputs: arrayPamphlet
*
************************************************** ***************************/
char *ReadString (char* filename, int *lengthPtr)
{
char * arrayPamphlet;
FILE * pFile;
int x;
*lengthPtr = 0;

/*Get filename from user*/
printf("\n\n Please enter a file name:");
scanf("%s",filename);

/*SEG FAULT IS HERE IF A VALID FILENAME IS ENTERED*/

/* Open the file*/
pFile = fopen(filename, "r");

/*Print error if file is empty*/
if (pFile == NULL)
{
printf("Can not open input file \n\n");
exit(-1);
}

/*FILE EXITS HERE IF AN INVALID FILENAME IS ENTERED*/

/*Get the number of characters in the file*/
while (fgetc(pFile) != EOF)
{
*lengthPtr = *lengthPtr + 1;
}

/*use malloc to dynamically allocate space for arrayPamphlet*/
arrayPamphlet = (char *) malloc (*lengthPtr * sizeof(char));

/*Go back to beginning of file*/
rewind(pFile);

/*Read data from file into arrayPamphlet*/
for (x = 0; x < *lengthPtr; x++)
{
arrayPamphlet[x] = fgetc(pFile);
}

return arrayPamphlet;
}
Nov 5 '06 #1
2 1259
figured it out....thx anyways
Nov 6 '06 #2
figured it out....thx anyways
Its working fine
just execute this code.
#include <stdio.h>
#include <stdlib.h>

char *ReadString (char* filename, int *lengthPtr)
{
char * arrayPamphlet;
FILE * pFile;
int x;
*lengthPtr = 0;

/*Get filename from user*/
printf("\n\n Please enter a file name:");
scanf("%s",filename);

/*SEG FAULT IS HERE IF A VALID FILENAME IS ENTERED*/

/* Open the file*/
pFile = fopen(filename, "r");

/*Print error if file is empty*/
if (pFile == NULL)
{
printf("Can not open input file \n\n");
exit(-1);
}

/*FILE EXITS HERE IF AN INVALID FILENAME IS ENTERED*/

/*Get the number of characters in the file*/
while (fgetc(pFile) != EOF)
{
*lengthPtr = *lengthPtr + 1;
}

/*use malloc to dynamically allocate space for arrayPamphlet*/
arrayPamphlet = (char *) malloc (*lengthPtr * sizeof(char));

/*Go back to beginning of file*/
rewind(pFile);

/*Read data from file into arrayPamphlet*/
for (x = 0; x < *lengthPtr; x++)
{
arrayPamphlet[x] = fgetc(pFile);
}

return arrayPamphlet;
}
int main()
{

int result;
char str[]="text";
char *ptr;
ptr = ReadString(str,&result);
printf("ptr=%s\n",ptr);

}
Nov 7 '06 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Anks | last post by:
i am unable to find why following code is giving segmentation fault.... way to produce seg fault: run the program... give input 12345678....enter any key except 'x'.... again give 12345678 as...
5
by: Ravikant | last post by:
Hello, Please check the following code .Let me know why its giving the segmentation fault while running it.First it asks to enter the name ,when entered it gives segmentation fault. But if I dont...
3
by: Zheng Da | last post by:
Program received signal SIGSEGV, Segmentation fault. 0x40093343 in _int_malloc () from /lib/tls/libc.so.6 (gdb) bt #0 0x40093343 in _int_malloc () from /lib/tls/libc.so.6 #1 0x40094c54 in malloc...
5
by: Fra-it | last post by:
Hi everybody, I'm trying to make the following code running properly, but I can't get rid of the "SEGMENTATION FAULT" error message when executing. Reading some messages posted earlier, I...
3
by: Moshe Kravchik | last post by:
Hi! We have a Web Service written in ATL Server and a client written in Java using Axis. When something goes wrong on the server side, it returns an HRESULT of the error which is translated into...
7
by: pycraze | last post by:
I would like to ask a question. How do one handle the exception due to Segmentation fault due to Python ? Our bit operations and arithmetic manipulations are written in C and to some of our...
14
by: Vlad Dogaru | last post by:
Hello, I am trying to learn C, especially pointers. The following code attempts to count the appearences of each word in a text file, but fails invariably with Segmentation Fault. Please help me...
3
by: =?Utf-8?B?TWFucHJlZXQgU3VzaGls?= | last post by:
I am having a Webservice within which i am throwing SOAP Exceptions and therefore whenever something wrong happens a SOAP fault comes up in the response - see below: <?xml version="1.0"...
2
by: George2 | last post by:
Hello everyone, In Process Explorer and in Perfmon, the page fault are both hard page fault, right? How to get the soft page fault value for a process? thanks in advance, George
3
by: jr.freester | last post by:
I have created to classes Matrix and System. System is made up of type matrix. ---------------------------------------------------------------------------------- class Matrix { private: int...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
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...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
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...

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.