Click here to Skip to main content
15,908,634 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi friends ..
I hAve a file contains number of line ..
My question is how I can use the function fgets in c language or another function maybe you know ..
I want to calculate the number of bytes of every line in another file
This example
Read line by line the file1 then my file2 will be :
234 : number bytes of line 1
321 : number bytes ...
...
Thanks for your helps

What I have tried:

File1 = fopen (``fl1``,``rb``);
File2 = open (fl2,wb);
Int line [128];
Int x ;
Int number=0;
While ( fgets ( line , size of line , file1)
{
Sscanf ( line , &x );
}
Number_line ++;
Posted
Updated 16-Nov-16 5:12am
Comments
Rick York 16-Nov-16 11:21am    
A few notes about what you have tried :
- the fopen call takes a file name (a string) and a mode (a string).
- You are reading and writing text files so the fopen mode strings should be "r" and "w", respectively. You don't want the 'b' in the strings because that signifies binary mode.
- There is no need to use sscanf in your loop. You want to count the number of characters in the line so strlen is a good candidate for that.
- Try fprintf for writing the byte counts.
yagami_md 16-Nov-16 11:49am    
How I can use strlen to determine the number of bytes in line?
Rick York 16-Nov-16 13:23pm    
Read the documentation for strlen. It will do the work for you.

If you can't figure that out then count the characters yourself. First read the docs on fgets so you know exactly what it returns.

Then keep trying things until you get it and that's the only way to really learn. You will not learn anything if we just tell you what to do.
yagami_md 17-Nov-16 9:58am    
Please can you help me
I want use fgets and strlen +
Please
Rick York 17-Nov-16 10:46am    
I believe the best way to learn this stuff is the way I did : read the docs, read example code of the functions, and try it yourself. I am NOT going to write this for you. You should be able to figure it out for yourself. This also gives you the opportunity to learn the most valuable tool you have: the debugger.

1 solution

There are two ways:
1) Read each line using fgets and write out the length. That's trivial - it's a standard null terminated string so count the characters up to the null and you can print it.
2) Read the whole file into memory, and scan it. For each line, keep a counter of the number of characters and look for the newline code (depending on your system that might be '\r', '\n', or '\r' followed by '\n'). When you find it, print the number of characters, and reset the count.

Neither is particularly difficult, it's just a question of personal preference.
But ... this is your homework, so code is all down to you!
 
Share this answer
 
Comments
yagami_md 16-Nov-16 12:40pm    
Char c ;
Number count=0;
While ( c=fgetc( File 1)) != EOF)
{
If ( c==`\n`)
{
Fprintf ( file 2,``%d``\n``,number Count);
Number Count++;
}
Else
Number count++;
}

I tried that but in file I see only one number in line
3
5
7
What the problem?
OriginalGriff 16-Nov-16 14:01pm    
Well... look at your while condition.
It's not going to compile - partly because it's got an upper case While but C is case sensitive, partly because it's got more ')' than '(' - and it really depends on what the code you actually ran looked like!
jeron1 16-Nov-16 12:53pm    
How can it do anything? it doesn't compile.
yagami_md 16-Nov-16 15:46pm    
I corrected it .. now it compile but really don't give me the right solution
jeron1 16-Nov-16 15:53pm    
You should post it then, along with an explanation of what its doing versus what you expect it to do. Are you familiar with debuggers?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900