Reputation: 11
I am trying to understand how to read some file from text, and then enter it into an array to save it for later manipulation. I just cant seem to get it. I have tried structs and string arrays but I still cant grasp how to implement this in c. I have successfully been able record at least one line into the array but it copies the same line multiple times. Here is a layout of what my text and code look like.
Text File:
# A demonstration of some simple MIPS instructions
Loop: sll $t1, $s3, 2
add $t1, $t1, $s6
lw $t0, 0($t1)
bne $t0, $s5, Exit
addi $s3, $s3, 1
j Loop
Exit:
.c File:
int assem::readFile(FILE *file) //read file function for file to be read
{
//char String[200];
char lines[20];
char *ptr = lines;
const int MAX = 101;
char myString[MAX];
int i = 0;
while(fgets(&lines[i], 100, file) != NULL)
{
i++;
}
printf("%s\n", lines);
Upvotes: 1
Views: 212
Reputation: 1450
Here's a simple code to statically store 10 lines containing 99 characters (don't forget \0 to terminate a string)
const size_t maxLines = 10;
size_t currentLine = 0;
char lines[maxLines][100]; // Storing 10 lines
// Reading and storing a line from file
while(fgets(lines[currentLine], sizeof(lines[currentLine]), file) && currentLine < maxLines)
{
++currentLine;
}
// Printing stored lines
size_t i = 0;
while (i < currentLine)
{
printf("%s", lines[i]);
++i;
}
Upvotes: 1
Reputation: 11
Thanks for the help> i managed to solve figure out how to do it a bit earlier. Here is my implementation after I finally understood a few more c literals. Thanks AGAIN!!! "counter" keeps track of how many lines are in the text file
int assem::readFile(FILE *file)
{
int i =0;
counter = 0; //this is sort of the constructor, if you will.
//breaks down text file into line by line
while(fgets(myString, 101, file) != NULL) //and stores them into "myLines array"
{
myLines[i] = strdup(myString);
i++;
counter++;
}
return 0;
}
Upvotes: -1
Reputation: 86
You need to read all the file character by character. It will make it easier.
#include <stdio.h>
void main ()
{
int ch, size, i = 0;
FILE *file;
file = fopen("Text.txt", "r"); // open the file for reading
fseek(file, 0L, SEEK_END); // goto the end of the file
size = ftell(file);
rewind(file); // goto the start of the file
char *text = (char*)malloc(size - 20); // the -20 is to avoid the gibberish at the end of the text file
for (i = 0; i<size; i++)
{
ch = fgetc(file);
text[i] = ch;
}
printf("%s", text);
getchar();
}
Upvotes: 0
Reputation: 5629
char * fgets ( char * str, int num, FILE * stream );
fgets()
reads characters from stream
and stores them as a C string into str
until (num-1
) characters have been read or either a newline or the end-of-file is reached, whichever happens first.
I have successfully been able record at least one line
Because a newline character makes fgets stop reading. See reference here.
You can use
size_t fread ( void * ptr, size_t size, size_t count, FILE * stream );
to read from a file.
fread() reads an array of count
elements, each one with a size of size
bytes, from the stream
and stores them in the block of memory specified by ptr
.
I write the following function to read from a file and write each character into a buffer, which may help you:
typedef unsigned char BYTE;
void readFile(char *filename, BYTE* buffer) {
FILE * pFile;
long lSize;
size_t result;
pFile = fopen(filename, "rb");
if (pFile == NULL ) {
fputs("File error", stderr);
exit(1);
}
// obtain file size:
fseek(pFile, 0, SEEK_END);
lSize = ftell(pFile);
rewind(pFile);
// copy the file into the buffer:
result = fread(buffer, 1, lSize, pFile);
if (result != lSize) {
fputs("Reading error 2", stderr);
exit(3);
}
/* the whole file is now loaded in the memory buffer. */
fclose(pFile);
}
Upvotes: 1