File input output please help :(

Thread Solved

Join Date: Dec 2008
Posts: 2
Reputation: Excess3 is an unknown quantity at this point 
Solved Threads: 0
Excess3 Excess3 is offline Offline
Newbie Poster

File input output please help :(

 
0
  #1
Dec 27th, 2008
Hello ,, i need to write to opened file in w+ mode..
now the problem is that first off all i need to check first string in each line if he the same to value i want to enter , dont enter and print error
:
in another words i need to convert this input txt to data txt:
33333333 Johnny Depp center 6  
44444444 Sandra Bullock guard 5
77777777 Tom Cruise captain 2
88888888 Cameron Diaz  other 2
22222222 Gwyneth Paltrow alto 2
the first number is ID of person.. before i putting it to my data.txt
i need to check if ID alredy exist : how i do that?


What i am now using is this function to take line into string
  1. char *getline(FILE *input)
  2. {
  3. char temp[MAX_LEN];
  4. fgets(temp,MAX_LEN,input);
  5. temp[strlen(temp)-1]='\0';
  6. return strdup(temp);
  7.  
  8. }

lets say *buffer=getline(Pointer to data file)
ID=strtok(buffer," ");
strcmp(ID_original,ID)
But i have problems with it , lets say if the data file is empty and there is nothing to scan i got errors
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: File input output please help :(

 
0
  #2
Dec 27th, 2008
> lets say if the data file is empty and there is nothing to scan i got errors
Well you need to pay attention to the return result of fgets() before you do anything else.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 2,030
Reputation: Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of 
Solved Threads: 177
Aia's Avatar
Aia Aia is offline Offline
Postaholic

Re: File input output please help :(

 
0
  #3
Dec 27th, 2008
temp[strlen(temp)-1]='\0'; assumes that there's always a '\n' at the end of line, and that could be not the case.

  1. /* wanted MAX_LEN of data, is there something to read? */
  2. if (fgets(temp, MAX_LEN, input) != NULL) {
  3. size_t net_len = strlen(temp) -1;
  4.  
  5. /* trim the newline/return if it's there */
  6. if (temp[net_len] == '\n') {
  7. temp[net_len] = '\0';
  8. }
  9. } else {
  10. /* handle the error... */
  11. }
Last edited by Aia; Dec 27th, 2008 at 12:24 pm.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 1,405
Reputation: William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of 
Solved Threads: 114
Sponsor
William Hemsworth William Hemsworth is offline Offline
Nearly a Posting Virtuoso

Re: File input output please help :(

 
0
  #4
Dec 27th, 2008
return strdup(temp);
Bad idea, you should never return a pointer to allocated dynamic memory, as you can quite easily forget to release that memory once you've done with it. The correct way to do this is to add a parameter containing a pointer to a buffer which you can write to.

Instead of this:
  1. char *getline(FILE *input)
  2. {
  3. char temp[MAX_LEN];
  4. fgets(temp,MAX_LEN,input);
  5. temp[strlen(temp)-1]='\0';
  6. return strdup(temp);
  7. }
Try:
  1. int getline(FILE *input, char *ptr, int destSize)
  2. {
  3. char temp[MAX_LEN];
  4. /* wanted MAX_LEN of data, is there something to read? */
  5. if (fgets(temp, MAX_LEN, input) != NULL) {
  6. size_t net_len = strlen(temp) -1;
  7.  
  8. /* trim the newline/return if it's there */
  9. if (temp[net_len] == '\n') {
  10. temp[net_len] = '\0';
  11. }
  12. } else {
  13. /* handle the error... */
  14. }
  15. strcpy_s(ptr, destSize, temp);
  16. }
Hope this helps
Last edited by William Hemsworth; Dec 27th, 2008 at 4:10 pm.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: File input output please help :(

 
0
  #5
Dec 27th, 2008
> Bad idea, you should never return a pointer to allocated dynamic memory
What does malloc do then?

There's nothing inherently wrong with returning a duplicated copy of the string in allocated memory, so long as it's documented that the caller is responsible for calling free at some point.

The only dubious part of it was using the non-standard strdup() to do the copying.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 1,405
Reputation: William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of 
Solved Threads: 114
Sponsor
William Hemsworth William Hemsworth is offline Offline
Nearly a Posting Virtuoso

Re: File input output please help :(

 
0
  #6
Dec 27th, 2008
>There's nothing inherently wrong with returning a duplicated copy of the string in allocated memory

I will agree with that to some extent but only if the caller knows exactly what he's doing, as by doing this, it makes your code more prone to errors. I'm quite sure it's bad practice to do this as i've caused myself problems in the past with memory leaks. One of my most common mistakes was something similar to this:
  1. char *SubString(char *str, int beg, int fin) {
  2. char *sub = (char*)malloc( fin - beg + 1 );
  3. // Rest of the code
  4. return sub;
  5. }
  6.  
  7. int main() {
  8. // Oops
  9. someFunction( SubString("abcdef", 2, 4) );
  10. }
It's better to be safe than sorry in my opinion
Last edited by William Hemsworth; Dec 27th, 2008 at 6:50 pm.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 2,030
Reputation: Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of 
Solved Threads: 177
Aia's Avatar
Aia Aia is offline Offline
Postaholic

Re: File input output please help :(

 
0
  #7
Dec 27th, 2008
>It's better to be safe than sorry in my opinion
Don't touch that keyboard then.

As a serious note:
char *sub = (char*)malloc( fin - beg + 1 ); casting malloc() is not necessary if the proper header file is included.
Last edited by Aia; Dec 27th, 2008 at 8:27 pm.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 2
Reputation: Excess3 is an unknown quantity at this point 
Solved Threads: 0
Excess3 Excess3 is offline Offline
Newbie Poster

Re: File input output please help :(

 
0
  #8
Dec 28th, 2008
wow guys tnx ... nice forum also
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC