LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 11-21-2011, 04:03 AM   #16
nikhidet1
LQ Newbie
 
Registered: Oct 2011
Posts: 25

Original Poster
Rep: Reputation: Disabled

hey all,
i am trying to solve this problem for quite sometimes..hopefully some here can help me.....i even used the codes here to try and program...but somehow when i put other codes it give me problems.I used another program to create another text file and the mode is appending(fopen("text.txt","a")). i am trying to read this text file line by line and using function strtok i split the string line by line into 2 string...i include here both of my c programs,the text.txt, and the outputs after executing the programs.....



Please help me...im trying to solve this problem for quite sometimes and still couldnt manage to solve it...thanks in advanced

Last edited by nikhidet1; 11-21-2011 at 07:47 AM.
 
Old 11-21-2011, 04:12 AM   #17
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,852
Blog Entries: 1

Rep: Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868
Nor did you find the (code) and (/code) tags...
 
Old 11-21-2011, 04:18 AM   #18
nikhidet1
LQ Newbie
 
Registered: Oct 2011
Posts: 25

Original Poster
Rep: Reputation: Disabled
hey thanks for the quick reply but i couldnt understand what u means mate...
 
Old 11-21-2011, 04:37 AM   #19
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,852
Blog Entries: 1

Rep: Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868
The first problem with your first program:
Code:
p1.c:40: error: 'fp' undeclared (first use in this function)
And please put you sources between [code] and [/code] tags.
 
Old 11-21-2011, 06:34 AM   #20
nikhidet1
LQ Newbie
 
Registered: Oct 2011
Posts: 25

Original Poster
Rep: Reputation: Disabled
hey thanks again.... i have solve my problem...
 
Old 11-21-2011, 06:41 AM   #21
nikhidet1
LQ Newbie
 
Registered: Oct 2011
Posts: 25

Original Poster
Rep: Reputation: Disabled
could u help me with explanation...i still couldnt really understand ur codes...
i couldnt understand ur code from char **lines=NULL; till lines[nlines++]=stdup(line);....thanks in advanced..anyways u really help me out..thanks mate
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main (void)
{
FILE *fp;
char *line= NULL;
size_t buflen, linelen;
ssize_t read;
char **lines= NULL;
size_t nlines= 0, nmaxlines= 0;
size_t i;

fp = fopen("mytext.txt", "r");
if (fp == NULL) exit(EXIT_FAILURE);

line= NULL;
buflen= 0;
while ((read = getline (&line, &buflen, fp)) != -1) {
linelen= strlen (line);
if (linelen>0 && line[linelen-1]=='\n') line[--linelen]= '\0';

if (nlines==nmaxlines) {
nmaxlines= nmaxlines? 2*nmaxlines: 64;
lines= realloc (lines, nmaxlines*sizeof(char *));
}
lines [nlines++] = strdup (line);
}
free (line);

for (i=0; i<nlines; ++i) {
printf ("%d '%s'\n", (int)i+1, lines[i]);
}

for (i=0; i<nlines; ++i) {
free (lines[i]);
}

return 0;
}
 
Old 11-21-2011, 07:18 AM   #22
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,852
Blog Entries: 1

Rep: Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868
Obviously, I won't explain a program line-by-line. Ask a specific question, okay?
 
Old 11-21-2011, 07:21 AM   #23
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
nikhidet1, you've been asked several times to put your code between [code] and [/code]. This means to put [code] before the start of your code and [/code] after the end of your code. Please do this to preserve indentation and aid readability.
 
Old 11-21-2011, 07:49 AM   #24
nikhidet1
LQ Newbie
 
Registered: Oct 2011
Posts: 25

Original Poster
Rep: Reputation: Disabled
ok ..i will try to understand it ...thanks anyways..but i would like to ask permission from you .can i use some of your codes for my program?

Last edited by nikhidet1; 11-21-2011 at 07:59 AM.
 
Old 11-21-2011, 08:15 AM   #25
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,852
Blog Entries: 1

Rep: Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868
You forgot to address the person you meant, but it is 'yes', if you meant myself.
(I repeat: if you have specific questions, do ask them.)
 
Old 11-21-2011, 08:26 AM   #26
nikhidet1
LQ Newbie
 
Registered: Oct 2011
Posts: 25

Original Poster
Rep: Reputation: Disabled
yeah i refer to you NevemTeve.i got one thing to ask.why u choose to times nmaxlines with two and if the condition is not true why u choose to have nmaxlines is equal to 64?i am referring to line (nmaxlines= nmaxlines? 2*nmaxlines: 64..is there any specific reason?can i times the nmaxlines with 3 instead of 2 and can i replace 64 with other value....thanks in advanced...

Last edited by nikhidet1; 11-21-2011 at 08:28 AM.
 
Old 11-21-2011, 08:34 AM   #27
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,852
Blog Entries: 1

Rep: Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868
The only important thing is not to call realloc to often (waste of CPU). For start, allocating 64 pointers takes 256/512 bytes, which is almost nothing. Later, when we run out of space, we double the allocated memory -- it seems to be an overkill, but still, any given time less than the half of the memory is unused.

And, if we really want, after reading in the whole file, we can release the unused area with another realloc:

Code:
nmaxlines= nlines;
lines= realloc (lines, nmaxlines*sizeof(char *));
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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



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

All times are GMT -5. The time now is 06:22 AM.

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