variables overwritten


 
Thread Tools Search this Thread
Top Forums Programming variables overwritten
# 1  
Old 03-10-2009
variables overwritten

Hi, i have some problems with the following code:

Code:
char *tab_path[num_classifiers];
char *sep=" \t\n";
char line[MAXLINE];
char *p;
FILE * file;
int i = 0;

if(fgets(line,MAXLINE,file)!=NULL){
        if((p=strtok(line,sep))!=NULL)tab_path[i]=p;
        while((p=strtok(NULL,sep))!=NULL){
            i++;
            tab_path[i]=p;
        }
    }

if(fgets(line,MAXLINE,file)!=NULL){
......
}

when the second fgets is executed tab_path variable is overwritten by the content of line variable. why?
thanks in advance
# 2  
Old 03-10-2009
tab_path points to several places in the line variable. When you read new data into the line variable, tab_path reflects those changes as well.
# 3  
Old 03-10-2009
ok, how can i avoid it happens??
# 4  
Old 03-10-2009
You have to use the fields in tab_path first, then come back and read the next line.
Othwerwise you will have to create storage for the parsed fields, and strcpy each tab_path element into that storage.

If you have a big file, it makes more sense to parse each line into field, work with them then read a new line. IF you have to have the whole file in memory all at one time consider memory mapping the file. See beej's guide:
Memory Mapped Files
# 5  
Old 03-10-2009
ok, thanks
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Ssh2 key has been overwritten, need a way to restore

I had generated a ssh2 key on my AIX box, to receive files from other AIX and Linux systems. Key Name: id_ssh2_server.pub However this ssh2 key (both public and private keys) has been overwritten, while I was generating another ssh2 key. Now the earlier configured target systems are not able... (3 Replies)
Discussion started by: freakygs
3 Replies

2. Shell Programming and Scripting

How to preserve the value of a variable from being overwritten?

Hi All, I am new new to unix.com, I have a question related to shell scripting. We have a Oracle database backup shell script, which can be used for taking full, incremental & archive log backup based on the parameters passed. Within the script we export a variable as export... (5 Replies)
Discussion started by: veeresh_15
5 Replies

3. Shell Programming and Scripting

file is getting overwritten

Hello All, I am writing a bash script on Solaris O/S. I looping through an array. For each iteration, i connect to the datatabase and use select statement. Output of which is redirected to .CSV file. here is the code for it. output="loop.csv" elements=${#currency_pair} ... (3 Replies)
Discussion started by: arundhati_s
3 Replies

4. Solaris

overwritten rootdisk?

Hi, The dump device on my system was set to /dev/dsk/c0t0d0s7. I have done a savecore -Lv on the system which worked fine. I'm wondering have I overwritten the rootdisk here by mistake? The system is still up but will need to be rebooted due to an error on it. Will it come back up? ... (8 Replies)
Discussion started by: gwhelan
8 Replies

5. UNIX for Advanced & Expert Users

Data Recovery from file system overwritten with LVM.

Hey peeps, Here is somethin u might find interestin.... Is it possible to recover data from a partition which used to be an ext3 file sytem with some nice forgotten backups, which now is an lvm partion containg root partition of another OS. :) I couldn't create any mess better than this, can... (2 Replies)
Discussion started by: squid04
2 Replies

6. HP-UX

Critical files in /etc overwritten EMPTY!

The following files were wiped out - new empty files were left in their place. /etc/inittab, /etc/inetd.conf, and /etc/MANPATH The system is running HP-UX 11i v3 - Mar08. Anyone seen anything like this? Any ideas on a way to figure this out if it happens again or a suggested way to... (9 Replies)
Discussion started by: KEnglander
9 Replies

7. SCO

Overwritten /dev/root -recovery of SCO OpenServer 5.0.4

Due to my own stupidity I managed to overwrite my /dev/root device using dd (don't ask). Current state is - Have current backup created using cpio (command used was 'find . -mount -depth -print|cpio -ocB > $TAPE') - Once I realised what was happening powered off the server but this was too... (2 Replies)
Discussion started by: ok2
2 Replies

8. AIX

UIDs being overwritten immediately

We have a problem where we delete a user and their associated UID gets dumped back in the UID pool. The if we immediately create a another (new) user, AIX reuses the last UID, the one that was just released. This is causing a problem when reports are being generated because the new users name is... (2 Replies)
Discussion started by: xsys2000
2 Replies

9. UNIX for Dummies Questions & Answers

Grub Loader entry overwritten

Hello, One of my frend had a problem. He had Windows XP installed on his system. Then he installed Red Hat Linux 8.0 in one of the partitions. After some time his XP got corrupt and then he reinstalled Windows XP. This over wrote the Grub loader entry, and due to this the grub loader is not... (2 Replies)
Discussion started by: rahulrathod
2 Replies

10. UNIX for Advanced & Expert Users

.cshrc and .login overwritten !!

Hi, My account is : abcd I belong to a group: pqrs Some thing straneg happened yesterday. My .cshrc and .login got overwritten into pqrs's .cshrc and .login I obviously did not explicitly overwrite pqrs's .cshrc. Are there any reasons how this could have happened indirectly due to... (5 Replies)
Discussion started by: gjthomas
5 Replies
Login or Register to Ask a Question