Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
I have a text file where every row has the format
A B:C
I want to read it using C language, but all i want to get in every line is what comes after the ':'. In other words I want to get only the C value in every line of the file, but all I want to do is read the file line-by-line and get every row's values.
Any idea to do that?
open the file for reading
fp = fopen("file.txt" , "r");
Use fgets() to read the line
fgets (str, MAX_LENGTH, fp);
Use Strtok to break according to ":"
ptr = strtok(str,":");
Read file line by line by using fgets()
fgets(line,MAX_SIZE,file_stream);
And use strchr(), to get the position from where C starts
char *ptr=strchr(line,':'); //ptr points to the : location
//now if you Move ptr location next to ':' ptr points to C
printf("C=%s",ptr+1);
And with out using additional pointer, You can do like this
printf("C=%s",strchr(line,':')+1);
Repeat these steps until reaching end of file. By checking the return value of fgets() against NULL.
Note: In this A B:C , if A or B consists : then You need to Apply logic accordingly.
You can also use strrchr()
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I'm trying to write a very simple win32 program. I open a file for reading using CreateFile(), and then read it's content using ReadFile()
HANDLE hfile=CreatFileW(L"Capturejpg.jpg", GENERIC_READ, 0,
NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,NULL);
bReadResult=ReadFile(hFile, ReadBuff, BUFFERSIZE-1, &dwBytes, &OL);
when I'm reading a .txt file for example, this works just fine, the problem is when the file I am reading contains NULL (which is the case with most files) then ReadFile function stops reading(or maybe writing bytes to ReadBuff) when reaching the first NULL.
before asking I searched and found two answers.
use something other that char array and char *: for this, I don't know what else I can use, cause my goal is to read the file and search for the files extension(for example if it's .gif then the first 3 characters read "Gif")
change DCB: the problem with this one is that I have no idea what DCB is actually, how to change it, and change exactly what in it.
EDIT: other posts with the same problem: this one and this one
ReadFile does not care one bit about the content that it reads. It will quite happily read zero bytes and continue reading beyond that point in the file. It wouldn't be much use if it could not do that.
You have just misdiagnosed the problem. You have read into a character array ReadBuffer and then printed like this:
printf("%s", ReadBuffer);
Now, printf will indeed stop when it reaches a zero byte. You will need to find some other way to output the content of this file.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
Fore example lets assume the following is a text in a file:
"I want to find number of characters from the first character is the nth occurrence of a character in a text file, but, I want to do this without declaring an array, storing the text file in it and applying the strchr function."
Lets say I want to find the position of the second new line character in the text? How many number of characters is the new line from the first character in the text? For e.g. The first occurrence character 't' is the 6th character of the text file.
If it is possible can someone please explain how? If no, can someone please explain why?
That can be done in an operating system that supports memory mapped files. This includes Windows and POSIX operating systems such as Linux.
In POSIX it is done using mmap(); there is example code in the documentation.
For Windows there is an API for for memory mapped files. Again, sample code is included in the documentation.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
This is part of a school project, so I can't show anybody my code, but suffice to say, my project has to read, copy, and edit its own source code (because we're studying polymorphic malware). I'm using fgets() to read each line of code, but whenever I have a line of code with a modulo operation or format string, the % symbol and whatever non-whitespace character that comes after it is missing. I'm guessing that fgets() is treating that % symbol like a format string, so how do I tell it not to do that?
Post answer edit:
I am posting one line of code to demonstrate what was wrong.
This is how I was getting strings from a file:
fgets(line, 128, src_file);
This is how I was writing that string to a new file (which was wrong)
fprintf(out_file, line);
If all you need is to write a string to a file you better use fputs():
fputs(line, out_file);
Your call to fprintf(out_file, line) is wrong.
When line contains the % character, it will be interpreted by fprintf() as a format specifier, which is not what you want.
You should change it to fprintf(out_file, "%s", line) instead.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
Can someone give me introduction of how to read text file in this order:
Line of text here
number1(startposition)
number2(endposition)
Line of text here
number1(startposition)
number2(endposition)
Line of text here
number1(startposition)
number2(endposition)
And then find substring of the line with text using number1 as startposition and number2 as endposition and copy the substring in new string.
OK, here is an introduction:
Read one line using fgets in a buffer str
Read 2 lines
Convert those 2 lines to numbers using strtol
Check that the indices make sense (i.e. that they are in the string)
Allocate a new string with length = end - start + 1
Use memcpy to copy end - start bytes from str + start
Make sure to add a 0-terminator to your destination string
Go back to first step
At each point when you do a fgets call be prepared for it to fail.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I want to write some data into a file. But I have too many data and each one should be named through a certain structure depending on the number of nodes and number of edges each data file has.
For example: for a data having 10 nodes and 20 edges, its name will be s_10_20.txt
for a data having 14 nodes and 30 edges, its name will be s_14_30.txt
the structure I'm following is: s_numOfNodes_numOfEdges.txt
(knowing that numOfNodes and numOfEdges are already scanned from the data file.)
The code I wrote is:
FILE *fp;
fp=fopen("s_%d_%d.txt",numOfNodes,numOfEdges,"w+");
This is giving me the following errors:
passing argument 2 of fopen makes pointer from integer without a cast
too many arguments to function fopen
How can I write it in any other way?
sprintf function can help you.
char file_name[MAX_PATH];
sprintf(file_name, s_%d_%d.txt",numOfNodes, numOfEdges);
fopen(file_name, "w+");