LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-29-2011, 06:20 PM   #1
psio15
LQ Newbie
 
Registered: Mar 2011
Posts: 1

Rep: Reputation: 0
Quick Pointer Issue (C)


Here is the issue. I am reading in a outside text file and putting in the string on that line into a char array that is already allocated.

Code:
int main(int argc, char *argv[])
{
	FILE *fp;
	fp = fopen(argv[1], "r");
	int theString = atoi(argv[2]);
	if(fp == NULL)
	{
		printf("Cannot open %s\n", argv[1]);
	}
	/*
	/ Ptr is allocated 1000 "lines" 50 "spaces" wide, all the size of char
	*/
	char *ptr = (char *)malloc(1000*50*sizeof(char));
	char buffer[100];
	int dict[1000];
	int i = 0;
	
		//Get next line and put into ptr with length of 50 chars of the line
	while(fgets(buffer, sizeof buffer, fp) != NULL)
	{
		dict[i] = convertString(buffer);
		
		//Currently keeps resetting the current line to ptr not adding on... fail on my part!
		*ptr++ = buffer;
		i++;
	}
	
	printf("%c\n", ptr[0]);
	
	// THIS FREE CREATES AN ABORT?!?!?!
	//free(ptr);
	return 0;
}
I am more than likely doing something simple, very wrong. The buffer is the current char[] that holds the line's string and I want to add that to the ptr.

Basically to put it in an analogy I am copying page for page from one "book" to another. In this case the text file to the array. Thanks for any help!
 
Old 03-30-2011, 05:00 AM   #2
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948
Quote:
Originally Posted by psio15 View Post
Code:
	/*
	/ Ptr is allocated 1000 "lines" 50 "spaces" wide, all the size of char
	*/
	char *ptr = (char *)malloc(1000*50*sizeof(char));
	char buffer[100];
	int dict[1000];
        int i = 0;
So ptr is your data array, buffer is a temporary buffer you use to read the lines into, dict is a hash array of sorts (with convertString() being the hash function), and i being the line number (starting from zero). I'll omit the dict and convertString() bits below to make the issue clearer.

The next bit is the problem:
Quote:
Originally Posted by psio15 View Post
Code:
	while(fgets(buffer, sizeof buffer, fp) != NULL)
	{
		*ptr++ = buffer;
		i++;
	}
You do read the line (assuming it is less than 99 characters including the newline, otherwise it'll take more than one call to read the entire line -- so it'd be a good idea to set a bit bigger size to buffer, just in case). However, you don't check if you read too many lines, and you just copy the first char of buffer to the ptr, and increase ptr[I], making it very difficult to retrieve the actual start of ptr. Later on, you try to free a pointer within the allocated array, which is obviously an error. Do not modify ptr itself, use some kind of an offset (50*i perhaps).

I believe you should use strspn() and strcspn() to skip the leading and trailing newlines and control characters (and whitespace), and then memcpy() to copy the interesting bit (not exceeding the allocated space) to the ptr array -- or write your own "copy trimmed string" function. The details depend on whether you want the ptr array to be a two-dimensional character array (without end-of-string zero bytes), or an array of strings (each with an end-of-string zero byte).

If you want to use a dynamic array of strings, consider the following:
Code:
#include <stdlib.h>
#include <string.h>

int       lines_max = 0;
int       lines     = 0;
char    **line      = NULL;
/* line[i] exists, if (i >= 0 && i < lines). */

/** addline() - Add a new line to the line array
 * @string      Pointer to the string
 * @length      Length to copy
 * Return value will be the line number, or negative if out of memory.
*/
int add_line(char const *const string, size_t const length)
{
    /* Make sure line array has room for another line. */
    if (lines >= lines_max) {
        size_t const   max = (size_t)lines + (size_t)512;
        char         **tmp;

        tmp = realloc(line_ptr, sizeof(char *), max);
        if (!tmp)
            return -1;

        line  = tmp;
        lines = (int)max;
     }

     /* Allocate line buffer */
     line[lines] = malloc(length + (size_t)1);
     if (!line[lines])
        return -1;

     /* Copy string */
     memcpy(line[lines], string, length);
     line[lines][length] = 0;

     /* Increase lines, but return the prior value. */
     return lines++;
}
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Quick question on using unclutter to hide Xorg mouse pointer d2army Linux - Software 3 03-02-2012 07:49 PM
Mouse pointer issue with Puppy and LUbuntu justapup Linux - Newbie 2 06-05-2010 06:47 PM
Writing a C++ Smart Pointer program and having an issue with my = operator Corps Programming 9 09-07-2009 08:08 PM
Mouse Pointer Speed Issue moe_b59 Linux - Hardware 1 02-05-2006 02:16 PM
Help with pointer issue simonkaos Linux - General 1 06-05-2003 01:10 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:21 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration