help with opening files and reading them in C


 
Thread Tools Search this Thread
Top Forums Programming help with opening files and reading them in C
# 8  
Old 02-10-2011
The script (apart from changing str_len to strlen) seems to work - such as it is.

Knowing nothing about the format of this "words" file, do you not think that allocating ten characters to each word is maybe on the stingy side (char line[11];?
# 9  
Old 02-10-2011
well i dont want to give a fixed number of allocating, as the string could be any length, and the str_len is my own function that gives the length of the string, excluding the \0 and \n at the end, so still i dont know whats wrong. if i loop through the word list and print the lines normally, it comes out right.....
# 10  
Old 02-10-2011
And what, exactly, does you own str_len function give you thats greater than what fgets will read?

You gave a "fixed number of allocating" when you wrote char line[11].

It seems clear that you would get "extra blank lines" where you have ten-character words:

Code:
$ cat file1
1234567890
12345678901

$ ./main   
1234567890
10 1


1 2
1234567890
10 3
1

2 4

# 11  
Old 02-10-2011
ok changing that 11 to 30 fixed it, but now after i do that i get some kind of error that never happened before
Code:
*** stack smashing detected ***: ./myprog terminated
======= Backtrace: =========
/lib/tls/i686/cmov/libc.so.6(__fortify_fail+0x48)[0xb7e7b238]
/lib/tls/i686/cmov/libc.so.6(__fortify_fail+0x0)[0xb7e7b1f0]
./myprog [0x8048cfd]
[0xb7000000]

# 12  
Old 02-10-2011
You used 11 in two places. Did you change it in both?
# 13  
Old 02-10-2011
haha thanks that fixed it.

but the only thing bugging me is that i changed it to 30, assuming the biggest string is 30 bytes, is there no generalised way to allocate the size of the biggest string or something?, like what if a string thats more then 30 comes up?
# 14  
Old 02-10-2011
What's the biggest word in the dictionary?

Google it Smilie

Would it hurt just to make it 1000? It's not like you're storing every word in the dictionary - you're getting them one at a time.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Loop Script and not opening files containing spaces

Hello, I wrote a simple script, that basically wait for a *.dat-file in a certain folder, which is always a zipped file and extracts it. It worked before and i changed nothing in the script, but since last week i have the problem, that it doesnt extract files containing a space. How do i make... (4 Replies)
Discussion started by: blend_in
4 Replies

2. Shell Programming and Scripting

Perl-opening a file then copying files

Good morning guys!! Im still practicing with Perl and now Im trying to open a file, and copy its contents to another file. Them I want to remeove the information out of the orginal file after it is copied over. The flow should be messages-->messages1-->messages2. Kind of like a log... (1 Reply)
Discussion started by: bigben1220
1 Replies

3. Shell Programming and Scripting

Opening Mulitple files using For loop in Perl

Hi All, I have a total of ten file to open in the Perl script and i am using a for loop to open each file and capture some strings inside each file. Unfortunately, i encounter the below syntax error. I think there should be something wrong with this term reports_${counting}_${_}.txt but i do... (4 Replies)
Discussion started by: Raynon
4 Replies

4. Web Development

opening text files with javascript

Im aware under IE, javascript uses ActiveX Objects to open up and manipulate text files, excel files etc.. How abouts would I open a text file using javascript in Firefox ? thanks (1 Reply)
Discussion started by: JamesGoh
1 Replies

5. Shell Programming and Scripting

error while opening files in a directory

I tried opening the files in a directory and print thier names. But it was thowing the error "unable to open directorty at t5.pl line 2." Can u plz correct this error in below mentioned code. #!/data/scoredev/perl opendir(HOMEDIR,"c:\vin") || die("unable to open directorty");... (3 Replies)
Discussion started by: vinay123
3 Replies

6. Shell Programming and Scripting

Opening Files as command line arguments

Hi have set a command line argument where the user enters the filename. I have set a condition that if the command line is null (user just enters return), the script will exit (with an error message). However, what if the user types something and the file isn't found. How will I go about... (20 Replies)
Discussion started by: Cactus Jack
20 Replies

7. UNIX for Dummies Questions & Answers

Opening Files in append mode

Is there any other way to open a file in append mode without using C calls or >> redirection? (1 Reply)
Discussion started by: vidhya
1 Replies

8. UNIX for Dummies Questions & Answers

opening non-html files in lynx??

when i try to open a txt file in lynx I need to provide the filename or use wildcards to open. Autocompletion doesn't work for some reason. Also, trying to open files like: .sh, .py etc. ends up in the following error: lynx: Start file could not be found or is not text/html or text/plain ... (0 Replies)
Discussion started by: riwa
0 Replies

9. UNIX for Dummies Questions & Answers

Opening files

I am very new to unix. I want to open a file and read one line in at a time. Can anybody help? (3 Replies)
Discussion started by: saarshad001
3 Replies

10. UNIX for Dummies Questions & Answers

Opening Files

I'm a new to UNIX/LINUX. I just put cygwin on my laptop and I can navigate around the directories, but I can't open files (.doc, .ppt, .html or .exe). Is there an explicit command to do this? I know that in Solaris when it does not recognize the file, it brings up the list of available viewing... (4 Replies)
Discussion started by: AJA
4 Replies
Login or Register to Ask a Question